|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
D: V; g' r7 L0 o4 L- S一、实验任务和目的 / I4 l; F5 N! N8 E( }& z8 C9 y' w
1. 掌握Matlab的字符串常用函数及其操作方法。 ' T- u3 o% H5 t l3 ?" x
2. 掌握Matlab的结构体的基本操作方法。
4 r/ q" B1 W3 ~! o* c. j# ^2 h3. 掌握Matlab的元胞数组的基本操作方法。 . P- C6 @- l% |
二、实验内容 1 O# A, e# W5 W) f* Q' L
1. 字符串数组Str=[‘hopes, dreams, hold up, old up’],查找’O’出现的次数和位置。
5 Z5 E6 [* l0 s2 Z! Z" f9 n2. 现有三个字符串变量s1=“i”,s2=“love”,s3=“matlab7.1”,利用字符串处理函数,将其用空格连接在一起,并字母转换为大写,并将7.1替换为2016a。 ( R$ C' F$ Q4 z+ B: h
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.’,对该字符串做如下处理:
/ b8 _; v( b1 D2 s Y! R# ^(1)判断字符串中每个单词的首字母是否大写,若不是则将其修改为大写,其他字母为小写。 " K( ^! ^& ~# H/ k$ A" _1 o
(2)统计字符串中的数字和字母的个数。
5 c# s' q8 `9 h* T(3)将字符串中间的空格和数字删除,所有字母倒过来重新排序。
& d6 T7 |5 a+ u' ?8 U4. 创建一个结构体,用于统计学生的情况,包括学生的姓名、学号、各科成绩等。然后使用该结构体对一个班级的学生的成绩进行管理,如计算总分、平均分、排列名次等。
/ v- _$ ? C# L5. 创建一个2X2的元胞数组,第1、2个元素为字符串,第3元素为整型,第4元素为双精度类型,并将其用图形表示。
* P; l4 s3 ^" w
0 s2 b8 K% G1 V三、实验过程和结果 / k: b& T. u+ R% x7 _" k# J' }/ c
1. 字符串数组Str=[‘hopes, dreams, hold up, old up’],查找’O’出现的次数和位置。
; W3 S; x' z$ y {# x' m# U0 Y( F' B5 p3 P
- >> Str=['hopes, dreams, hold up, old up'];
- p=findstr(Str,'O')
- p =
- []
- >> length(p)
- ans =
- 05 @9 l; d3 P+ w n) f! e$ ?
( k0 T# m$ N5 t2 C) y1 F; m
% D* u7 X4 T# q& ?- M. h, b& L0 E; \2 . 现有三个字符串变量s1=“i”,s2=“love”,s3=“matlab7.1”,,并字母转换为大写,并将7.1替换为2016a。
5 R3 k3 `; ?3 Z" F9 u: f) Y r) u9 T/ D# W
- >> 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, z2 M* ~3 E5 _6 F9 H6 |7 ^
" d4 o" T. F7 y$ s; ?
$ ]; }% D2 p7 |+ }
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.’,对该字符串做如下处理: / ]- I7 G8 h; X# Z
(1)判断字符串中每个单词的首字母是否大写,若不是则将其修改为大写,其他字母为小写。, l! q9 u* ~- g3 G) I
+ e m8 y0 x3 t: T! l. \' ?* k+ E
- >> 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.
( R; ` M8 k0 N D7 t; ~# ^ ! [# n8 Q& @) a5 S; V2 ]9 V* _, ]
4 L- n, r9 Z( n/ n9 K
(2)统计字符串中的数字和字母的个数。) V8 @; t6 Y1 w8 }4 s
/ ?) }- q- C: V% E6 v2 P- >> 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 ]' f# ^* b3 u 5 J2 @+ K% c; n( r6 L1 b
8 R6 M0 h- Q; e" M4 @/ g u
(3)将字符串中间的空格和数字删除,所有字母倒过来重新排序。
" E$ Q2 F, v/ V2 m+ i; J$ K- [6 ~7 n- ~9 E' N/ X
- >> 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
w4 c4 [, g$ n/ f" q2 N! l( [ 3 G# R# Z- S' h# { j/ r
8 K& O8 _$ |/ O8 H5 J' e% V4 创建一个结构体,用于统计学生的情况,包括学生的姓名、学号、各科成绩等。然后使用该结构体对一个班级的学生的成绩进行管理,如计算总分、平均分、排列名次等。# \ m- A& ^9 b$ U5 m
: f1 g# h% h/ R7 v: I4 O这里假设一个班有三名同学(不好意思,这个我暂时不会), M: f7 e/ D3 h" W1 P' D
* j5 F' `! W) }/ ?" Z/ |5 创建一个2X2的元胞数组,第1、2个元素为字符串,第3元素为整型,第4元素为双精度类型,并将其用图形表示。
0 i. z- h# k+ w' b
+ b" q- ^; \: c; q- >> 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)& l* c$ j1 S8 X, B
, Q* e$ L" e: L
. G" ~6 W G. E) F8 m
, M3 o* d& c% E- H8 k- ?; X' P
四、实验总结和心得 ; ~" H P4 K, C# s Q
掌握了字符串的查找,连接,删除,倒置,替换等一系列操作
% q; Y8 o+ T% D) Z! ]" I1 Q掌握了结构数组和元胞数组的用法
3 w! _! J( x1 C' n( ?. z' _" n2 h% l5 j9 z) ?
) L/ m" d3 Y7 p0 h8 m! N+ _% r; V" u* G5 H) J, E% ~
6 X7 ?0 E: x/ j5 |
|
|