ch03字符串胞元和构架数组.docx
《ch03字符串胞元和构架数组.docx》由会员分享,可在线阅读,更多相关《ch03字符串胞元和构架数组.docx(34页珍藏版)》请在冰豆网上搜索。
ch03字符串胞元和构架数组
第3章字符串、胞元和构架数组
.1MATLAB的数据类型
图3.1-1MATLAB的数据类型
.2字符串数组
本节内容集中于字符串数组(以下简称串数组)。
与数值数组相比,串数组在MATLAB中的重要性较小,但它不可缺少。
假如没有串数组及相应的操作,那么数据可视化将会遇到困难,构作MATLAB的宏指令也将会遇到困难。
既然字符串与数值是两种不同的数据类(Class),那么它们的创建方式也就不同。
数值变量是通过在指令窗中数字赋值创建的。
而字符变量的创建方式是:
在指令窗中,先把待建的字符放在“单引号对”中,再按[Enter]键。
注意,这“单引号对”必须在英文状态下输入。
这“单引号对”是MATLAB识别送来内容“身份”(是变量名、数字,还是字符串)所必需的。
MATLAB为数值数组设计了许多运算、函数和操作,但既没对串数组设计那么多运算,也没设计那么多操作。
然而正是这些串函数和串操作,为MATLAB的文字表达、复杂字符的组织、宏功能的发挥提供了有力的支持。
.2.1串数组的属性和标识
【例3.2-1】本例演示:
串的基本属性、标识和简单操作。
(1)
a='Thisisanexample.'
a=
Thisisanexample.
(2)
size(a)
ans=
119
(3)
a14=a(1:
4)
ra=a(end:
-1:
1)
a14=
This
ra=
.elpmaxenasisihT
(4)
Ua=double(a)
Ua=
Columns1through13
8410410511532105115329711032101120
Columns14through19
9710911210810146
char(Ua)
ans=
Thisisanexample.
(5)
w=find(a>='a'&a<='z');
Ua(w)=Ua(w)-32;
char(Ua)
ans=
THISISANEXAMPLE.
(6)
A='这是一个算例。
';
A_s=size(A)
A56=A([56])
UCA=double(A)
A_s=
17
A56=
算例
UCA=
Columns1through6
368252615919968200103163920363
Column7
12290
char(UCA)
ans=
这是一个算例。
(7)
b='Example''3.2-1'''
b=
Example'3.2-1'
(8)
ab=[a(1:
7),'',b,'.']%这里第2个输入为空格串
ab=
ThisisExample'3.2-1'.
.2.2复杂串数组的创建
1多行字符串数组的创建
【例3.2-2】演示:
多行串数组的“补空等长”直接输入法;非等长串数组借助char,str2mat,strvcat等指令声称多行串数组。
(1)
clear
S=['Thisstringarray'
'hasmultiplerows.']
size(S)
S=
Thisstringarray
hasmultiplerows.
ans=
218
(2)
S1=char('Thisstringarray','hastworows.')
size(S1)
S1=
Thisstringarray
hastworows.
ans=
217
S2=str2mat('这','字符','串数组','','由4行组成')
size(S2)
S2=
这
字符
串数组
由4行组成
ans=
55
S3=strvcat('这','字符','串数组','','由4行组成')
size(S3)
S3=
这
字符
串数组
由4行组成
ans=
45
2利用胞元数组创建复杂字符串
【例3.2-3】胞元数组在存放和操作字符串上的应用。
(1)
C1={'MATLAB201xaincludesdatatypes:
';
'◆Doublearray';
'◆Characterarray';
'andsoon'}%<4>
C1_c=class(C1)
size(C1)%<6>
C1=
'MATLAB201xaincludesdatatypes:
'
'◆Doublearray'
'◆Characterarray'
'andsoon'
C1_c=
cell
ans=
41
(2)借助cellstr指令生成存放复杂字符串的胞元数组
C2=char('MATLAB201xaincludesdatatypes:
',...
'◆Doublearray',...
'◆Characterarray',...
'andsoon')%<10>
C2_c=class(C2)
size(C2)
C2=
MATLAB201xaincludesdatatypes:
◆Doublearray
◆Characterarray
andsoon
C2_c=
char
ans=
432
C3=cellstr(C2)
size(C3)
C3=
'MATLAB201xaincludesnewdatatypes:
'
'◆Doublearray'
'◆Characterarray'
'andsoon'
ans=
41
.2.3串转换函数
【例3.2-4】最常用的数组/字符串转换函数int2str,num2str,mat2str示例。
(1)
A=eye(2,4);
A_str1=int2str(A)
A_str1=
1000
0100
(2)
rng(0,'v5uniform')
B=rand(2,4);
B3=num2str(B,3)
B3=
0.950.6070.8910.456
0.2310.4860.7620.0185
(3)
B_str=mat2str(B,4)
B_str=
[0.95010.60680.89130.4565;0.23110.4860.76210.0185]
Expression=['exp(-',B_str,')'];
eval(Expression)
ans=
0.38670.54510.41010.6335
0.79370.61510.46670.9817
【例3.2-5】fprintf,sprintf,sscanf的用法示例。
rng(0,'v5uniform');a=rand(2,2);
s1=num2str(a)
s_s=sprintf('%.10e\n',a)
s1=
0.950130.60684
0.231140.48598
s_s=
9.5012928515e-001
2.3113851357e-001
6.0684258354e-001
4.8598246871e-001
fprintf('%.5g\\',a)
0.95013\0.23114\0.60684\0.48598\
s_str2=str2num(s_s)
s_sscan=sscanf(s_s,'%f',[3,2])
s_str2=
0.9501
0.2311
0.6068
0.4860
s_sscan=
0.95010.4860
0.23110
0.60680
【例3.2-6】double,str2double,str2num的异同。
(1)
a='1e-3';
da=double(a)
sda=str2double(a)
sna=str2num(a)
da=
491014551
sda=
1.0000e-003
sna=
1.0000e-003
(2)
b='1,2;3,4';
db=double(b)
sdb=str2double(b)
snb=str2num(b)
db=
49445059514452
sdb=
NaN
snb=
12
34
(3)
c='ab+cde';
dc=double(c)
sdc=str2double(c)
snc=str2num(c)
dc=
97984399100101
sdc=
NaN
snc=
[]
(4)
d=sym('1/3');
dd=double(d)
sdd=str2double(d)
snd=str2num(d)
dd=
0.3333
sdd=
NaN
?
?
?
Errorusing==>str2numat33
Requiresstringorcharacterarrayinput.
【例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))];%<7>
y_text=['y=',num2str(y_max)];%<8>
max_text=char('maximum',t_text,y_text);%<9>
%
tit=['y=exp(-',num2str(a),'t)*sin(',num2str(w),'t)'];%<11>
plot(t,zeros(size(t)),'k')
holdon
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)%<16>
title(tit),xlabel('t'),ylabel('y'),holdoff
图3.1-1字符串运用示意图
.2.4串操作函数
.3胞元数组
.3.1胞元数组的创建和显示
1胞元标识寻访和内容编址寻访的不同
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%<5>
A=
[2x10char][3x3double]
[1.0000+2.0000i][1x1sym]
(2)
B{1,1}=C_str;B{1,2}=R;B{2,1}=Cn;B{2,2}=S_sym;
celldisp(B)%<7>
B{1,1}=
这是
胞元数组创建算例1
B{2,1}=
1.0000+2.0000i
B{1,2}=
147
258
369
B{2,2}=
-sin(3*t)/exp(t)
.3.2胞元数组的扩充、收缩和重组
【例3.3-2】胞元数组的扩充,及形象化图示胞元内容指令cellplot。
(1)
C=cell
(2);
C(:
1)={char('Another','textstring');10:
-1:
1}
C=
[2x11char][]
[1x10double][]
(2)胞元数组的“列”扩充和“行”扩充
AC=[AC]
A_C=[A;C]
AC=
[2x10char][3x3double][2x11char][]
[1.0000+2.0000i][1x1sym][1x10double][]
A_C=
[2x10char][3x3double]
[1.0000+2.0000i][1x1sym]
[2x11char][]
[1x10double][]
cellplot(A_C,'legend')
图3.3-1胞元数组A_C的形象化结构图
【例3.3-3】采用“空”对胞元数组进行收缩操作,采用reshape对胞元数组进行重组。
(1)
A_C(3,:
)=[]
A_C=
[2x10char][3x3double]
[1.0000+2.0000i][1x1sym]
[1x10double][]
(2)
R_A_C=reshape(A_C,2,3)
R_A_C=
[2x10char][1x10double][1x1sym]
[1.0000+2.0000i][3x3double][]
.3.3胞元数组内容的获取和配置
【例3.3-4】本例演示:
胞元和胞元内容获取的区别;花括号、圆括号的功用;多胞元内容配置的deal指令法和直接配置法。
(1)
f1=R_A_C(1,3)
class(f1)
f1=
[1x1sym]
ans=
cell
(2)
f2=R_A_C{1,3}
class(f2)
f2=
sin(-3*t)*exp(-t)
ans=
sym
(3)
f3=R_A_C{1,1}(:
[1256])
f3=
这是
胞元创建
(4)
[f4,f5,f6]=deal(R_A_C{[1,3,4]})
f4=
这是
胞元数组创建算例1
f5=
10987654321
f6=
147
258
369
(5)
[ff4,ff5,ff6]=R_A_C{[1,3,4]}
ff4=
这是
胞元数组创建算例1
ff5=
10987654321
ff6=
147
258
369
.3.4胞元与数值数组之间的转换
【例3.3-5】常用胞元数组转换函示例。
(1)
rng(0,'v5uniform')
A=rand(2,3)
C1=num2cell(A)
SC1=size(C1)
A=
0.95010.60680.8913
0.23110.48600.7621
C1=
[0.9501][0.6068][0.8913]
[0.2311][0.4860][0.7621]
SC1=
23
C2=num2cell(A,2)
disp(['C2{1,:
}=',mat2str(C2{1,:
},4)])
SC2=size(C2)
C2=
[1x3double]
[1x3double]
C2{1,:
}=[0.95010.60680.8913]
SC2=
21
C3=num2cell(A,1)
SC3=size(C3)
C3=
[2x1double][2x1double][2x1double]
SC3=
13
(2)
x=zeros(4,5);x(:
)=1:
20
C4=mat2cell(x,[22],[32])
celldisp(C4)
x=
1591317
26101418
37111519
48121620
C4=
[2x3double][2x2double]
[2x3double][2x2double]
C4{1,1}=
159
2610
C4{2,1}=
3711
4812
C4{1,2}=
1317
1418
C4{2,2}=
1519
1620
(3)
D1=cell2mat(C4(1,:
))
D2=[C4{1,1},C4{1,2}]
D1=
1591317
26101418
D2=
1591317
26101418
.3.5对胞元数组运算的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.50000.4859
ans=
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=
[1x4sym][1x5double]
[10000x3double][]
B=cellfun(@mean,[y(1,1),y(1,2),y(2,1)],'UniformOutput',false);
celldisp(B)
B{1}=
pi/4
B{2}=
1.5708
B{3}=
0.49860.50420.4977
C=cellfun(@mean,[y(1,2),y(2,1)])
?
?
?
Errorusing==>cellfun
Non-scalarinUniformoutput,atindex2,output1.
Set'UniformOutput'tofalse.
.3.6胞元数组的操作函数汇总
.4构架数组
.4.1构架数组的创建和显示
1直接创建法及显示
【例3.4-1】本例通过温室数据(包括温室名、容积、温度、湿度等)演示:
单构架的创建和显示。
(1)
GreenHouse.name='一号房';%<1>
GreenHouse.volume='2000立方米';%<2>
GreenHouse.parameter.temperature=[31.230.431.628.7
29.731.130.929.6];%<3>
GreenHouse.parameter.humidity=[62.159.557.761.5
62.061.959.257.5];%<4>
(2)
GreenHouse%<5>
GreenHouse=
name:
'一号房'
volume:
'2000立方米'
parameter:
[1x1struct]
GreenHouse.parameter%<6>
ans=
temperature:
[2x4double]
humidity:
[2x4double]
GreenHouse.parameter.temperature%<7>
ans=
31.200030.400031.600028.7000
29.700031.100030.900029.6000
【例3.4-2】本例演示:
构架数组的创建和显示,并利用构架数组保存一个温室群的数据。
本例的运行以例3.4-1为先导。
(1)
GreenHouse(2,3).name='六号房'%<1>
GreenHouse=
2x3structarraywithfields:
name
volume
parameter
(2)
GreenHouse(1,1)%<3>
ans=
name:
'一号房'
volume:
'2000立方米'
parameter:
[1x1struct]
2利用构造函数创建构架数组
【例3.4-3】利用构造函数struct,建立温室群的数据库。
(1)
a=cell(2,3);
gh1=struct('name',a,'volume',a,'parameter',a(1,2))%<2>
gh1=
2x3structarraywithfields:
name
volume
parameter
(2)
gh2=struct('name',a,'volume',[],'parameter',[])%<3>
gh2=
2x3structarraywithfields:
name
volume
parameter
(3)
gh3(2,3)=struct('name',[],'volume',[],'parameter',[])%<4>
gh3=
2x3structarraywithfields:
name
volume
parameter
(4)
a1={'六号房'};a2={'3200立方米'};
gh4(2,3)=struct('name',a1,'volume',a2,'parameter',[]);%<6>
T6=[31.2,30.4,31.6,28.7;29.7,31.1,30.9,29.6];%<7>
gh4