matlab字符串单元数组与结构体作业答案

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

1.编制一个脚本,查找给定字符串中指定字符出现的次数和位置;str=['hopes,dreams,holdup,holdup'];findstr(str,'o')%位置ans=21523a=findstr(str,'o');n=length(a)%次数n=32.编写一个脚本,判断输入字符串中每个单词的首字母是否为大写,若不是则将其修改为大写,其他字母为小写。str='ihaveadreamcometrue';nlength=length(str);fork=1:nlengthif(k==1||isspace(str(k-1)))&&(str(k)='z'&&str(k)='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.创建一个结构体,用于统计学生的情况,包括学生的姓名、学号、各科成绩等。然后使用该结构体对一个班级的学生成绩进行管理,如计算总分、平均分、排列名次等。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:namenumberscoressubjectstudent(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=246sum(student(2).scores)ans=261sum(student(3).scores)ans=224mean(student(1).scores)ans=82mean(student(2).scores)ans=87mean(student(3).scores)ans=74.6667[new,index]=sort([student.scores],'descend')new=939089858278777562index=462735918student(1).mean=mean(student(1).scores)student(2).mean=mean(student(2).scores)student(3).mean=mean(student(3).scores)student=1x3structarraywithfields:namenumberscoressubjectmeanstudent=1x3structarraywithfields:namenumberscoressubjectmeanstudent=1x3structarraywithfields:namenumberscoressubjectmeanstudent(1)ans=name:'bam'number:1scores:[758982]subject:'english,math,chinese'mean:82student(2)ans=name:'mark'number:2scores:[937890]subject:'english,math,chinese'mean:87student(3)ans=name:'jimin'number:3scores:[856277]subject:'english,math,chinese'mean:74.6667[new,index]=sort([student.mean],'descend')new=87.000082.000074.6667index=213

1 / 7
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功