资源描述:
《matlab字符串单元数组与结构体作业答案.docx》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、1.编制一个脚本,查找给定字符串中指定字符出现的次数和位置;str=['hopes,dreams,holdup,holdup'];>>findstr(str,'o')%位置ans=21523>>a=findstr(str,'o');n=length(a)%次数n=32.编写一个脚本,判断输入字符串中每个单词的首字母是否为大写,若不是则将其修改为大写,其他字母为小写。>>str='ihaveadreamcometrue';nlength=length(str);fork=1:nlengthif(k==1
2、
3、isspace(str(k-1)))&&(str(k)<='z'&&str(k)>=
4、'a')str(k)=char(double(str(k))-32);endenddisp(str);IHaveADreamComeTrue3.创建2×2单元数组,第1、2个元素为字符串,第三个元素为整型变量,第四个元素为双精度(double)类型,并将其用图形表示。>>A=cell(2,2)A(1,1)={'beijing'};A(2,1)={'BEIJING'};A(1,2)={uint8(5)};A(2,2)={[1,2;4,7]}cellplot(A)A=[][][][]A='beijing'[5]'BEIJING'[2x2double]4.创建一个结构体,用于统计学生的情况,包
5、括学生的姓名、学号、各科成绩等。然后使用该结构体对一个班级的学生成绩进行管理,如计算总分、平均分、排列名次等。>>student=struct('name',{'bam','mark','jimin'},'number',{1,2,3},'scores',{[75,89,82],[93,78,90],[85,62,77]},'subject',{'english,math,chinese','english,math,chinese','english,math,chinese'})student=1x3structarraywithfields:namenumberscoressubj
6、ect>>student(1)ans=name:'bam'number:1scores:[758982]subject:'english,math,chinese'>>student(2)ans=name:'mark'number:2scores:[937890]subject:'english,math,chinese'>>student(3)ans=name:'jimin'number:3scores:[856277]subject:'english,math,chinese'>>>>sum(student(1).scores)ans=246>>sum(student(2).scor
7、es)ans=261>>sum(student(3).scores)ans=224mean(student(1).scores)ans=82>>mean(student(2).scores)ans=87>>mean(student(3).scores)ans=74.6667>>[new,index]=sort([student.scores],'descend')new=939089858278777562index=462735918>>student(1).mean=mean(student(1).scores)student(2).mean=mean(student(2).scor
8、es)student(3).mean=mean(student(3).scores)student=1x3structarraywithfields:namenumberscoressubjectmeanstudent=1x3structarraywithfields:namenumberscoressubjectmeanstudent=1x3structarraywithfields:namenumberscoressubjectmean>>student(1)ans=name:'bam'number:1scores:[758982]subject:'english,math,chin
9、ese'mean:82>>student(2)ans=name:'mark'number:2scores:[937890]subject:'english,math,chinese'mean:87>>student(3)ans=name:'jimin'number:3scores:[856277]subject:'english,math,chinese'mean:74.6667>>[new,index]=sort([student