|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
' j/ R3 }* t* R L
一、实验任务和目的
9 Q3 O* _+ v3 q) R. n1. 掌握Matlab的字符串常用函数及其操作方法。 & K. a3 w0 j2 A6 {- d* A
2. 掌握Matlab的结构体的基本操作方法。 7 V0 h$ s5 G1 k6 d r! J
3. 掌握Matlab的元胞数组的基本操作方法。
6 x) L5 L8 J5 F9 \二、实验内容
6 w# j Z8 {1 {9 v. w# s+ |) Y1. 字符串数组Str=[‘hopes, dreams, hold up, old up’],查找’O’出现的次数和位置。 " a2 t- W- P6 x' `) ^) a+ F
2. 现有三个字符串变量s1=“i”,s2=“love”,s3=“matlab7.1”,利用字符串处理函数,将其用空格连接在一起,并字母转换为大写,并将7.1替换为2016a。 * L& R1 h4 {* C8 K1 Z
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.’,对该字符串做如下处理:
3 r% G8 ]- ] o) O(1)判断字符串中每个单词的首字母是否大写,若不是则将其修改为大写,其他字母为小写。
7 {6 z* }, \ ^! R# F(2)统计字符串中的数字和字母的个数。
+ l1 }0 P. i' ~* N9 v(3)将字符串中间的空格和数字删除,所有字母倒过来重新排序。 1 d# U( c, G3 e! b: x7 O( x+ H
4. 创建一个结构体,用于统计学生的情况,包括学生的姓名、学号、各科成绩等。然后使用该结构体对一个班级的学生的成绩进行管理,如计算总分、平均分、排列名次等。
2 d; H* D% c8 @5. 创建一个2X2的元胞数组,第1、2个元素为字符串,第3元素为整型,第4元素为双精度类型,并将其用图形表示。
. b5 n6 L: D; O3 C" _- s9 [' {9 G& u' q8 | _! @; C# e; F' H; x
三、实验过程和结果
% ~! W/ \1 n6 V9 s, p5 r1. 字符串数组Str=[‘hopes, dreams, hold up, old up’],查找’O’出现的次数和位置。
9 |# B2 g y- T$ z1 Q9 R0 k. E, ~9 J6 g! o( y3 X$ K e
- >> Str=['hopes, dreams, hold up, old up'];
- p=findstr(Str,'O')
- p =
- []
- >> length(p)
- ans =
- 01 t; s0 [8 U* W: {" b
! h/ l, B* Y! [( w- l. L# L+ f+ n' e: m
2 . 现有三个字符串变量s1=“i”,s2=“love”,s3=“matlab7.1”,,并字母转换为大写,并将7.1替换为2016a。% K2 z$ D( {3 X, p9 V% y8 i9 ?
5 [- c! P$ d, K- o- >> 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';
- >> s4
- s4 =
- I LOVE MATLAB7.1
- >> strrep(s4,'7.1','2016a')
- ans =
- I LOVE MATLAB2016a
* m2 C, P& g3 B. h5 j! | 9 w+ S. u. h5 i* j
. z! O$ z1 ^/ B3 . 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.’,对该字符串做如下处理:
8 ~( c" o9 u1 C/ q3 I(1)判断字符串中每个单词的首字母是否大写,若不是则将其修改为大写,其他字母为小写。
7 {$ h J: R. i8 q( G7 z* |0 [5 |
2 G c. \" M& X' o- >> 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=findstr(Str,' ');
- l=length(k);
- for j=1:l
- if(Str(k(j)+1)>='a'&Str(k(j)+1)<='z')
- Str(k(j)+1)=Str(k(j)+1)-'a'+'A';
- end
- end
- >> Str
- 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." z6 H: L; @/ W, }0 q
1 B% ?8 U+ T2 c+ y( m* [
+ B: _1 L/ v' M) C: t
(2)统计字符串中的数字和字母的个数。
& }& |8 ~ o* S0 r
+ G6 e! P0 |" r0 s- >> 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+ d# \/ ^* G2 z& [
. X: Y1 q7 Z6 Q6 S0 d$ I5 Q
! g! y' b# Z" g6 x3 D% u! K(3)将字符串中间的空格和数字删除,所有字母倒过来重新排序。" j5 s. v, H8 p
0 N: c8 M9 M8 j, s- >> 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-retninodesabserudecorpdnasrosnessuoiravgnizilituybrianeponorehtegotlaroroodniyletelpmocrehtiegnikcartnoitacoltuobasihcraesergnitsixeehT& D0 Y( J3 f- }% o2 s& c
& H, L1 e/ ^9 `0 C- @ r1 a5 l
. P& d$ V; |( P2 P _4 创建一个结构体,用于统计学生的情况,包括学生的姓名、学号、各科成绩等。然后使用该结构体对一个班级的学生的成绩进行管理,如计算总分、平均分、排列名次等。
( Q- E7 x+ u0 g! k! Q
: H& a3 |4 y* Q. o0 h% D! b% J这里假设一个班有三名同学(不好意思,这个我暂时不会)
' b4 ]) L2 |& b5 g8 V+ u( W
3 @2 g9 ~# Y7 `4 i! _3 C9 L: f5 创建一个2X2的元胞数组,第1、2个元素为字符串,第3元素为整型,第4元素为双精度类型,并将其用图形表示。- }! Y m; ~+ M" S( z' C
5 O3 i+ F l: F O- >> 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)
" @$ k- T8 n. B ?" c
: A; U6 ~+ y' |! B; w# J* K0 h& s+ c
! k+ |: p+ V( D7 i1 m6 h: y8 M, Q+ f4 ]0 O O
四、实验总结和心得 7 M, f: P6 b9 |; I4 ~
掌握了字符串的查找,连接,删除,倒置,替换等一系列操作
, E/ j$ `+ L+ @掌握了结构数组和元胞数组的用法/ b$ H, L; @, W
+ t- W9 Z0 q i5 K
! ~- l" ^: x6 e; o* ]) d% k! H. p0 \3 j" [) H5 a
3 A' I, r, z- h" {( C; r9 }, c
|
|