2018.9.15,Matlab实验三:字符串、单元数组和结构体
一、實驗任務和目的
二、實驗內容
(1)判斷字符串中每個單詞的首字母是否大寫,若不是則將其修改為大寫,其他字母為小寫。
(2)統(tǒng)計字符串中的數字和字母的個數。
(3)將字符串中間的空格和數字刪除,所有字母倒過來重新排序。
三、實驗過程和結果
2 . 現有三個字符串變量s1=“i”,s2=“l(fā)ove”,s3=“matlab7.1”,,并字母轉換為大寫,并將7.1替換為2016a。
>> s1='i'; >> s2='love'; >> s3='matlab7.1'; >> s4=strcat(s1,32,s2,32,s3); >> k=find(s4>='a'&s4<='z'); >> s4(k)=s4(k)-'a'+'A'; >> s4s4 =I LOVE MATLAB7.1>> strrep(s4,'7.1','2016a')ans =I LOVE MATLAB2016a3 . Str=’ 1 The existing research is about location tracking either completely indoor or altogether on open air 2 by utilizing various sensors and procedures based on inter-networking or internet of things.’,對該字符串做如下處理:
(1)判斷字符串中每個單詞的首字母是否大寫,若不是則將其修改為大寫,其他字母為小寫。
(2)統(tǒng)計字符串中的數字和字母的個數。
>> Str=' 1 The existing research is about location tracking either completely indoor or altogether on open air 2 by utilizing various sensors and procedures based on inter-networking or internet of things.'; >> k=find(Str>='0'&Str<='9'); >> m=find(Str>='a'&Str<='z'); >> length(k)ans =2>> length(m)ans =162(3)將字符串中間的空格和數字刪除,所有字母倒過來重新排序。
>> Str=' 1 The existing research is about location tracking either completely indoor or altogether on open air 2 by utilizing various sensors and procedures based on inter-networking or internet of things.'; S=strrep(Str,' ','') k=find(S>='0'&S<='9'); S(k)=''; revch=S(end:-1:1)S =1Theexistingresearchisaboutlocationtrackingeithercompletelyindoororaltogetheronopenair2byutilizingvarioussensorsandproceduresbasedoninter-networkingorinternetofthings.revch =.sgnihtfotenretnirognikrowten-retninodesabserudecorpdnasrosnessuoiravgnizilituybrianeponorehtegotlaroroodniyletelpmocrehtiegnikcartnoitacoltuobasihcraesergnitsixeehT4 創(chuàng)建一個結構體,用于統(tǒng)計學生的情況,包括學生的姓名、學號、各科成績等。然后使用該結構體對一個班級的學生的成績進行管理,如計算總分、平均分、排列名次等。
這里假設一個班有三名同學(不好意思,這個我暫時不會)
5 創(chuàng)建一個2X2的元胞數組,第1、2個元素為字符串,第3元素為整型,第4元素為雙精度類型,并將其用圖形表示。
>> A=cell(2,2); >> A(1,1)={'i love'}; >> A(2,1)={'you'}; >> A(1,2)={int16(128)}; >> A(2,2)={double(16)}; >> cellplot(A)四、實驗總結和心得
掌握了字符串的查找,連接,刪除,倒置,替換等一系列操作
掌握了結構數組和元胞數組的用法
總結
以上是生活随笔為你收集整理的2018.9.15,Matlab实验三:字符串、单元数组和结构体的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 算法竞赛入门经典 第七章 总结
- 下一篇: mysql c语言数字转字符串函数_C+