matlab字符串单元数组与结构体作业答案.docx
《matlab字符串单元数组与结构体作业答案.docx》由会员分享,可在线阅读,更多相关《matlab字符串单元数组与结构体作业答案.docx(4页珍藏版)》请在冰豆网上搜索。
1.编制一个脚本,查找给定字符串中指定字符出现的次数和位置;
str=['hopes,dreams,holdup,holdup'];
>>findstr(str,'o')%位置
ans=
21523
>>a=findstr(str,'o');
n=length(a)%次数
n=
3
2.编写一个脚本,判断输入字符串中每个单词的首字母是否为大写,若不是则将其修改为大写,其他字母为小写。
>>str='ihaveadreamcometrue';
nlength=length(str);
fork=1:
nlength
if(k==1||isspace(str(k-1)))&&(str(k)<='z'&&str(k)>='a')
str(k)=char(double(str(k))-32);
end
end
disp(str);
IHaveADreamComeTrue
3.创建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:
name
number
scores
subject
>>student
(1)
ans=
name:
'bam'
number:
1
scores:
[758982]
subject:
'english,math,chinese'
>>student
(2)
ans=
name:
'mark'
number:
2
scores:
[937890]
subject:
'english,math,chinese'
>>student(3)
ans=
name:
'jimin'
number:
3
scores:
[856277]
subject:
'english,math,chinese'
>>
>>sum(student
(1).scores)
ans=
246
>>sum(student
(2).scores)
ans=
261
>>sum(student(3).scores)
ans=
224
mean(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=
939089858278777562
index=
462735918
>>student
(1).mean=mean(student
(1).scores)
student
(2).mean=mean(student
(2).scores)
student(3).mean=mean(student(3).scores)
student=
1x3structarraywithfields:
name
number
scores
subject
mean
student=
1x3structarraywithfields:
name
number
scores
subject
mean
student=
1x3structarraywithfields:
name
number
scores
subject
mean
>>student
(1)
ans=
name:
'bam'
number:
1
scores:
[758982]
subject:
'english,math,chinese'
mean:
82
>>student
(2)
ans=
name:
'mark'
number:
2
scores:
[937890]
subject:
'english,math,chinese'
mean:
87
>>student(3)
ans=
name:
'jimin'
number:
3
scores:
[856277]
subject:
'english,math,chinese'
mean:
74.6667
>>[new,index]=sort([student.mean],'descend')
new=
87.000082.000074.6667
index=