ImageVerifierCode 换一换
格式:DOCX , 页数:34 ,大小:68.08KB ,
资源ID:28886177      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/28886177.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(ch03字符串胞元和构架数组.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

ch03字符串胞元和构架数组.docx

1、ch03字符串胞元和构架数组第 3 章 字符串、胞元和构架数组.1 MATLAB的数据类型图3.1-1 MATLAB的数据类型.2 字符串数组 本节内容集中于字符串数组(以下简称串数组)。与数值数组相比,串数组在MATLAB中的重要性较小,但它不可缺少。假如没有串数组及相应的操作,那么数据可视化将会遇到困难,构作MATLAB的宏指令也将会遇到困难。 既然字符串与数值是两种不同的数据类(Class),那么它们的创建方式也就不同。数值变量是通过在指令窗中数字赋值创建的。而字符变量的创建方式是:在指令窗中,先把待建的字符放在“单引号对”中,再按Enter键。注意,这“单引号对”必须在英文状态下输入。

2、这“单引号对”是MATLAB识别送来内容“身份”(是变量名、数字,还是字符串)所必需的。 MATLAB为数值数组设计了许多运算、函数和操作,但既没对串数组设计那么多运算,也没设计那么多操作。然而正是这些串函数和串操作,为MATLAB的文字表达、复杂字符的组织、宏功能的发挥提供了有力的支持。.2.1 串数组的属性和标识【例3.2-1】本例演示:串的基本属性、标识和简单操作。(1)a=This is an example. a =This is an example. (2)size(a) ans = 1 19 (3)a14=a(1:4) ra=a(end:-1:1) a14 =Thisra =.

3、elpmaxe na si sihT (4)Ua=double(a) Ua = Columns 1 through 13 84 104 105 115 32 105 115 32 97 110 32 101 120 Columns 14 through 19 97 109 112 108 101 46 char(Ua) ans =This is an example. (5)w=find(a=a&a=z); Ua(w)=Ua(w)-32; char(Ua) ans =THIS IS AN EXAMPLE. (6)A=这是一个算例。; A_s=size(A) A56=A(5 6) UCA=dou

4、ble(A) A_s = 1 7A56 =算例UCA = Columns 1 through 6 36825 26159 19968 20010 31639 20363 Column 7 12290 char(UCA) ans =这是一个算例。 (7)b=Example 3.2-1 b =Example 3.2-1 (8)ab=a(1:7), ,b, . %这里第2个输入为空格串 ab =This is Example 3.2-1 . .2.2 复杂串数组的创建1 多行字符串数组的创建【例3.2-2】演示:多行串数组的“补空等长”直接输入法;非等长串数组借助char , str2mat , s

5、trvcat等指令声称多行串数组。(1)clearS=This string array has multiple rows.size(S) S =This string array has multiple rows.ans = 2 18 (2)S1=char(This string array,has two rows.)size(S1) S1 =This string arrayhas two rows. ans = 2 17 S2=str2mat(这,字符,串数组,由4行组成)size(S2) S2 =这 字符 串数组 由4行组成ans = 5 5 S3=strvcat(这,字符,串数

6、组,由4行组成)size(S3) S3 =这 字符 串数组 由4行组成ans = 4 5 2 利用胞元数组创建复杂字符串【例3.2-3】胞元数组在存放和操作字符串上的应用。(1)C1=MATLAB201xa includes data types:;Double array;Character array;and so on % C1_c=class(C1) size(C1) % C1 = MATLAB201xa includes data types: Double array Character array and so onC1_c =cellans = 4 1 (2)借助cellstr

7、指令生成存放复杂字符串的胞元数组C2=char(MATLAB201xa includes data types:,.Double array,.Character array,.and so on) % C2_c=class(C2)size(C2) C2 =MATLAB201xa includes data types:Double array Character array and so on C2_c =charans = 4 32 C3=cellstr(C2) size(C3) C3 = MATLAB201xa includes new data types: Double array

8、Character array and so onans = 4 1 .2.3 串转换函数【例3.2-4】最常用的数组/字符串转换函数int2str , num2str , mat2str 示例。(1)A=eye(2,4); A_str1=int2str(A) A_str1 =1 0 0 00 1 0 0 (2)rng(0,v5uniform)B=rand(2,4); B3=num2str(B,3) B3 = 0.95 0.607 0.891 0.4560.231 0.486 0.762 0.0185 (3)B_str=mat2str(B,4) B_str =0.9501 0.6068 0.8

9、913 0.4565;0.2311 0.486 0.7621 0.0185 Expression=exp(-,B_str,); eval(Expression) ans = 0.3867 0.5451 0.4101 0.6335 0.7937 0.6151 0.4667 0.9817 【例3.2-5】fprintf, sprintf, sscanf的用法示例。rng(0,v5uniform);a=rand(2,2); s1=num2str(a) s_s=sprintf(%.10en,a) s1 =0.95013 0.606840.23114 0.48598s_s =9.5012928515e-

10、0012.3113851357e-0016.0684258354e-0014.8598246871e-001 fprintf(%.5g,a) 0.950130.231140.606840.48598 s_str2=str2num(s_s)s_sscan=sscanf(s_s,%f,3,2) s_str2 = 0.9501 0.2311 0.6068 0.4860s_sscan = 0.9501 0.4860 0.2311 0 0.6068 0 【例3.2-6】double, str2double, str2num的异同。(1)a=1e-3;da=double(a) sda=str2double

11、(a) sna=str2num(a) da = 49 101 45 51sda = 1.0000e-003sna = 1.0000e-003 (2)b=1,2;3,4;db=double(b) sdb=str2double(b) snb=str2num(b) db = 49 44 50 59 51 44 52sdb = NaNsnb = 1 2 3 4 (3)c=ab+cde;dc=double(c) sdc=str2double(c) snc=str2num(c) dc = 97 98 43 99 100 101sdc = NaNsnc = (4)d=sym(1/3);dd=double(d

12、) sdd=str2double(d) snd=str2num(d) dd = 0.3333sdd = NaN? Error using = str2num at 33Requires string or character array input. 【例3.2-7】综合例题:在MATLAB计算生成的图形上标出图名和最大值点坐标。clear a=2; w=3; t=0:0.01:10; y=exp(-a*t).*sin(w*t); y_max,i_max=max(y); t_text=t=,num2str(t(i_max); % y_text=y=,num2str(y_max); % max_

13、text=char(maximum,t_text,y_text);% %tit=y=exp(-,num2str(a),t)*sin(,num2str(w),t); % plot(t,zeros(size(t),k) hold on plot(t,y,b) plot(t(i_max),y_max,r.,MarkerSize,20) text(t(i_max)+0.3,y_max+0.05,max_text) % title(tit),xlabel(t),ylabel(y),hold off 图 3.1-1 字符串运用示意图.2.4 串操作函数.3 胞元数组.3.1 胞元数组的创建和显示1 胞元标

14、识寻访和内容编址寻访的不同2 胞元数组的创建和显示【例 3.3-1】本例演示:胞元数组的创建。C_str=char(这是,胞元数组创建算例 1); R=reshape(1:9,3,3); Cn=1+2i; S_sym=sym(sin(-3*t)*exp(-t); (1)A(1,1)=C_str;A(1,2)=R;A(2,1)=Cn;A(2,2)=S_sym;A % A = 2x10 char 3x3 double 1.0000 + 2.0000i 1x1 sym (2)B1,1=C_str;B1,2=R;B2,1=Cn;B2,2=S_sym;celldisp(B) % B1,1 =这是 胞元数

15、组创建算例 1B2,1 = 1.0000 + 2.0000iB1,2 = 1 4 7 2 5 8 3 6 9B2,2 =-sin(3*t)/exp(t) .3.2 胞元数组的扩充、收缩和重组【例3.3-2】胞元数组的扩充,及形象化图示胞元内容指令cellplot。(1)C=cell(2); C(:,1)=char(Another,text string);10:-1:1 C = 2x11 char 1x10 double (2)胞元数组的“列”扩充和“行”扩充AC=A C A_C=A;C AC = 2x10 char 3x3 double 2x11 char 1.0000 + 2.0000i

16、1x1 sym 1x10 double A_C = 2x10 char 3x3 double 1.0000 + 2.0000i 1x1 sym 2x11 char 1x10 double cellplot(A_C,legend) 图 3.3-1 胞元数组A_C的形象化结构图【例3.3-3】采用“空”对胞元数组进行收缩操作,采用reshape对胞元数组进行重组。(1)A_C(3,:)= A_C = 2x10 char 3x3 double 1.0000 + 2.0000i 1x1 sym 1x10 double (2)R_A_C=reshape(A_C,2,3) R_A_C = 2x10 cha

17、r 1x10 double 1x1 sym 1.0000 + 2.0000i 3x3 double .3.3 胞元数组内容的获取和配置【例3.3-4】本例演示:胞元和胞元内容获取的区别;花括号、圆括号的功用;多胞元内容配置的deal指令法和直接配置法。(1)f1=R_A_C(1,3) class(f1) f1 = 1x1 symans =cell (2)f2=R_A_C1,3 class(f2) f2 =sin(-3*t)*exp(-t)ans =sym (3)f3=R_A_C1,1(:,1 2 5 6) f3 =这是 胞元创建 (4)f4,f5,f6=deal(R_A_C1,3,4) f4

18、=这是 胞元数组创建算例 1f5 = 10 9 8 7 6 5 4 3 2 1f6 = 1 4 7 2 5 8 3 6 9 (5)ff4,ff5,ff6=R_A_C1,3,4 ff4 =这是 胞元数组创建算例 1ff5 = 10 9 8 7 6 5 4 3 2 1ff6 = 1 4 7 2 5 8 3 6 9 .3.4 胞元与数值数组之间的转换【例3.3-5】常用胞元数组转换函示例。(1)rng(0,v5uniform)A=rand(2,3) C1=num2cell(A) SC1=size(C1) A = 0.9501 0.6068 0.8913 0.2311 0.4860 0.7621C1

19、= 0.9501 0.6068 0.8913 0.2311 0.4860 0.7621SC1 = 2 3 C2=num2cell(A,2) disp(C21,:=,mat2str(C21,:,4) SC2=size(C2) C2 = 1x3 double 1x3 doubleC21,:=0.9501 0.6068 0.8913SC2 = 2 1 C3=num2cell(A,1) SC3=size(C3) C3 = 2x1 double 2x1 double 2x1 doubleSC3 = 1 3 (2)x=zeros(4,5);x(:)=1:20 C4=mat2cell(x, 2 2, 3 2

20、)celldisp(C4) x = 1 5 9 13 17 2 6 10 14 18 3 7 11 15 19 4 8 12 16 20C4 = 2x3 double 2x2 double 2x3 double 2x2 doubleC41,1 = 1 5 9 2 6 10C42,1 = 3 7 11 4 8 12C41,2 = 13 17 14 18C42,2 = 15 19 16 20 (3)D1=cell2mat(C4(1,:) D2=C41,1,C41,2 D1 = 1 5 9 13 17 2 6 10 14 18D2 = 1 5 9 13 17 2 6 10 14 18 .3.5 对胞

21、元数组运算的cellfun指令【例3.3-6】演示cellfun的两种调用格式。一致性输出调用格式的限制很严格,而非一致性输出的调用格式适应性很强。(1)rng(1)x(1)=1:10;x(2)=rand(100,1); xm=cellfun(mean,x) class(xm) xm = 5.5000 0.4859ans =double (2)y(1,1)=sym(0,pi/6,pi/3,pi/2);y(1,2)=0:pi/4:pi;y(2,1)=rand(10000,3);y(2,2)=;B=cellfun(x)sin(x-pi/6).2,y,UniformOutput,false)B =

22、1x4 sym 1x5 double 10000x3 double B=cellfun(mean,y(1,1),y(1,2),y(2,1),UniformOutput,false);celldisp(B) B1 =pi/4B2 = 1.5708B3 = 0.4986 0.5042 0.4977 C=cellfun(mean,y(1,2),y(2,1)? Error using = cellfunNon-scalar in Uniform output, at index 2, output 1.Set UniformOutput to false. .3.6 胞元数组的操作函数汇总.4 构架数

23、组.4.1 构架数组的创建和显示1 直接创建法及显示【例3.4-1】本例通过温室数据(包括温室名、容积、温度、湿度等)演示:单构架的创建和显示。(1)GreenHouse.name=一号房; % GreenHouse.volume=2000立方米; %GreenHouse.parameter.temperature=31.2 30.4 31.6 28.7 29.7 31.1 30.9 29.6; %GreenHouse.parameter.humidity=62.1 59.5 57.7 61.5 62.0 61.9 59.2 57.5; % (2)GreenHouse % GreenHouse

24、 = name: 一号房 volume: 2000立方米 parameter: 1x1 struct GreenHouse.parameter % ans = temperature: 2x4 double humidity: 2x4 double GreenHouse.parameter.temperature % ans = 31.2000 30.4000 31.6000 28.7000 29.7000 31.1000 30.9000 29.6000 【例3.4-2】本例演示:构架数组的创建和显示,并利用构架数组保存一个温室群的数据。本例的运行以例3.4-1为先导。(1)GreenHous

25、e(2,3).name=六号房 % GreenHouse = 2x3 struct array with fields: name volume parameter (2)GreenHouse(1,1) % ans = name: 一号房 volume: 2000立方米 parameter: 1x1 struct 2 利用构造函数创建构架数组【例3.4-3】利用构造函数struct,建立温室群的数据库。(1)a=cell(2,3); gh1=struct(name,a,volume,a,parameter,a(1,2) % gh1 = 2x3 struct array with fields:

26、 name volume parameter (2)gh2=struct(name,a,volume,parameter,) % gh2 = 2x3 struct array with fields: name volume parameter (3)gh3(2,3)=struct(name,volume,parameter,) % gh3 = 2x3 struct array with fields: name volume parameter (4)a1=六号房;a2=3200立方米;gh4(2,3)=struct(name,a1,volume,a2,parameter,); % T6=31.2,30.4,31.6,28.7;29.7,31.1,30.9,29.6; % gh4

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1