书籍:Matlab实用教程
工具:Matlab2021a
电脑信息:Intel® Xeon® CPU E5-2603 v3 @ 1.60GHz
系统类型:64位操作系统,基于X64的处理器 windows10 专业版
第2章 MATLAB数值计算
2.5 元胞数组和结构数组
2.5.1 元胞数组
1、元胞数组的创建
A={'This is the first cell.',[1 2;3 4],eye(3),{'Tom','Jane'}} A = { [1,1] = This is the first cell. [1,2] = 1 2 3 4 [1,3] = Diagonal Matrix 1 0 0 0 1 0 0 0 1 [1,4] = { [1,1] = Tom [1,2] = Jane } } B{1,1}={'This is the second cell.'}; B{1,2}={5+3*i}; B{1,3}={[1 2;3 4;5 6]} B = { [1,1] = { [1,1] = This is the second cell. } [1,2] = { [1,1] = 5 + 3i } [1,3] = { [1,1] = 1 2 3 4 5 6 } }2、元胞数组的内容显示
B{1,1}={'This is the second cell.'}; B{1,2}={5+3*i}; B{1,3}={[1 2;3 4;5 6]}; celldisp(B) B B{1}{1} = This is the second cell. B{2}{1} = 5 + 3i B{3}{1} = 1 2 3 4 5 6 B = { [1,1] = { [1,1] = This is the second cell. } [1,2] = { [1,1] = 5 + 3i } [1,3] = { [1,1] = 1 2 3 4 5 6 } } B{1,1}={'This is the second cell.'}; B{1,2}={5+3*i}; B{1,3}={[1 2;3 4;5 6]}; cellplot(B)3、元胞数组的内容获取
B{1,1}={'This is the second cell.'}; B{1,2}={5+3*i}; B{1,3}={[1 2;3 4;5 6]}; x1=B{1,2} x2=B{1,3} [x3,x4,x5]=deal(B{[1 2 3]}) x1 = { [1,1] = 5 + 3i } x2 = { [1,1] = 1 2 3 4 5 6 } x3 = { [1,1] = This is the second cell. } x4 = { [1,1] = 5 + 3i } x5 = { [1,1] = 1 2 3 4 5 6 }2.5.2 结构数组
1、结构数组的创建
ps(1).name='curve1' ps(1).color='red' ps(1).position=[0,0,300,300] ps = scalar structure containing the fields: name = curve1 ps = scalar structure containing the fields: name = curve1 color = red ps = scalar structure containing the fields: name = curve1 color = red position = 0 0 300 300 ps(2)=struct('name','curve2','color','blue','position',[100,100,300,300]) ps = 1x2 struct array containing the fields: name color position2、结构数组数据的获取和设置
ps(2)=struct('name','curve2','color','blue','position',[100,100,300,300]) x1=ps(2) x2=ps(2).position x3=ps(2).position(1,3) x4=getfield(ps,{2},'color') x5=getfield(ps,{2},'color',{1}) ps=setfield(ps,{2},'color','green'); ps(2) ps = 1x2 struct array containing the fields: name color position x1 = scalar structure containing the fields: name = curve2 color = blue position = 100 100 300 300 x2 = 100 100 300 300 x3 = 300 x4 = blue x5 = b ans = scalar structure containing the fields: name = curve2 color = green position = 100 100 300 3003、结构数组域的获取
ps(1)=struct('name','curve1','color','red','position',[0,0,300,300]) ps(2)=struct('name','curve2','color','blue','position',[100,100,300,300]) x6=fieldnames(ps) all_x=[ps.name] cat(1,ps.position) cat(2,ps.position) cat(3,ps.position) ps = scalar structure containing the fields: name = curve1 color = red position = 0 0 300 300 ps = 1x2 struct array containing the fields: name color position x6 = { [1,1] = name [2,1] = color [3,1] = position } all_x = curve1curve2 ans = 0 0 300 300 100 100 300 300 ans = 0 0 300 300 100 100 300 300 ans = ans(:,:,1) = 0 0 300 300 ans(:,:,2) = 100 100 300 3002.6 数据分析
2.6.1 数据统计和相关分析
X=[5.30 13.00 0.40;5.10 11.80 -1.70;3.70 8.10 0.60;1.50 7.70 -4.50] max(X) min(X) mean(X) std(X) median(X) var(X) C=cov(X) S=corrcoef(X) [S,k]=sort(X,1) X = 5.3000 13.0000 0.4000 5.1000 11.8000 -1.7000 3.7000 8.1000 0.6000 1.5000 7.7000 -4.5000 ans = 5.3000 13.0000 0.6000 ans = 1.5000 7.7000 -4.5000 ans = 3.9000 10.1500 -1.3000 ans = 1.7512 2.6489 2.3735 ans = 4.4000 9.9500 -0.6500 ans = 3.0667 7.0167 5.6333 C = 3.0667 4.0867 3.0667 4.0867 7.0167 2.7100 3.0667 2.7100 5.6333 S = 1.0000 0.8810 0.7378 0.8810 1.0000 0.4310 0.7378 0.4310 1.0000 S = 1.5000 7.7000 -4.5000 3.7000 8.1000 -1.7000 5.1000 11.8000 0.4000 5.3000 13.0000 0.6000 k = 4 4 4 3 3 2 2 2 1 1 1 32.6.2 差分和积分
A=[5.30 13.00 0.40;5.10 11.80 -1.70;3.70 8.10 0.60;1.50 7.70 -4.50] diff(A,1,1) gradient(A) sum(A) cumsum(A,2) cumprod(A,2) trapz(A) cumtrapz(A) A = 5.3000 13.0000 0.4000 5.1000 11.8000 -1.7000 3.7000 8.1000 0.6000 1.5000 7.7000 -4.5000 ans = -0.2000 -1.2000 -2.1000 -1.4000 -3.7000 2.3000 -2.2000 -0.4000 -5.1000 ans = 7.7000 -2.4500 -12.6000 6.7000 -3.4000 -13.5000 4.4000 -1.5500 -7.5000 6.2000 -3.0000 -12.2000 ans = 15.6000 40.6000 -5.2000 ans = 5.3000 18.3000 18.7000 5.1000 16.9000 15.2000 3.7000 11.8000 12.4000 1.5000 9.2000 4.7000 ans = 5.3000 68.9000 27.5600 5.1000 60.1800 -102.3060 3.7000 29.9700 17.9820 1.5000 11.5500 -51.9750 ans = 12.2000 30.2500 -3.1500 ans = 0 0 0 5.2000 12.4000 -0.6500 9.6000 22.3500 -1.2000 12.2000 30.2500 -3.1500 t=0:0.5:10; y=exp(-0.2).*sin(t) d=[0 diff(y)] s1=0.5*cumsum(y) s2=cumtrapz(t,y) y = Columns 1 through 8: 0 0.3925 0.6889 0.8167 0.7445 0.4900 0.1155 -0.2872 Columns 9 through 16: -0.6196 -0.8003 -0.7851 -0.5776 -0.2288 0.1761 0.5379 0.7680 Columns 17 through 21: 0.8100 0.6537 0.3374 -0.0615 -0.4454 d = Columns 1 through 8: 0 0.3925 0.2964 0.1277 -0.0722 -0.2545 -0.3744 -0.4027 Columns 9 through 16: -0.3324 -0.1807 0.0152 0.2075 0.3489 0.4049 0.3618 0.2301 Columns 17 through 21: 0.0420 -0.1563 -0.3163 -0.3989 -0.3839 s1 = Columns 1 through 8: 0 0.1963 0.5407 0.9491 1.3213 1.5663 1.6241 1.4805 Columns 9 through 16: 1.1707 0.7705 0.3779 0.0891 -0.0253 0.0628 0.3317 0.7157 Columns 17 through 21: 1.1207 1.4476 1.6163 1.5856 1.3629 s2 = Columns 1 through 8: 0 0.0981 0.3685 0.7449 1.1352 1.4438 1.5952 1.5523 Columns 9 through 16: 1.3256 0.9706 0.5742 0.2335 0.0319 0.0188 0.1973 0.5237 Columns 17 through 21: 0.9182 1.2842 1.5320 1.6009 1.47422.6.3 卷积和快速傅里叶变换
A=ones(1,10); A(1)=0 B=ones(1,9); B([1 2 3])=0 C=conv(A,B) N=32; AF=fft(A,N) BF=fft(B,N) CF=AF.*BF CC=real(ifft(CF)) A = 0 1 1 1 1 1 1 1 1 1 B = 0 0 0 1 1 1 1 1 1 C = 0 0 0 0 1 2 3 4 5 6 6 6 6 5 4 3 2 1 AF = Columns 1 through 4: 9.0000 + 0i 4.3815 - 6.5574i -1.9239 - 4.6447i -1.5927 - 0.3168i Columns 5 through 8: 0.7071 - 0.7071i -0.3960 - 1.9910i -1.3827 - 0.5727i -0.1285 + 0.0858i Columns 9 through 12: 0 - 1.0000i -1.0704 - 0.7152i -0.6173 + 0.2557i 0.0642 - 0.3228i Columns 13 through 16: -0.7071 - 0.7071i -0.9039 + 0.1798i -0.0761 + 0.1838i -0.3542 - 0.5300i Columns 17 through 20: -1.0000 + 0i -0.3542 + 0.5300i -0.0761 - 0.1838i -0.9039 - 0.1798i Columns 21 through 24: -0.7071 + 0.7071i 0.0642 + 0.3228i -0.6173 - 0.2557i -1.0704 + 0.7152i Columns 25 through 28: 0 + 1.0000i -0.1285 - 0.0858i -1.3827 + 0.5727i -0.3960 + 1.9910i Columns 29 through 32: 0.7071 + 0.7071i -1.5927 + 0.3168i -1.9239 + 4.6447i 4.3815 + 6.5574i BF = Columns 1 through 4: 6.0000 + 0i 2.6719 - 4.9988i -2.6310 - 3.9375i -3.3624 + 0.3312i Columns 5 through 8: -0.7071 + 1.7071i 0.2625 + 0.3199i -0.6756 + 0.1344i -0.3805 + 1.2542i Columns 9 through 12: 1.0000 + 1.0000i 1.0293 - 0.3122i 0.0898 - 0.4514i 0.1710 + 0.1403i Columns 13 through 16: 0.7071 - 0.2929i 0.1005 - 1.0200i -0.7832 - 0.5233i -0.4923 + 0.2632i Columns 17 through 20: 0 + 0i -0.4923 - 0.2632i -0.7832 + 0.5233i 0.1005 + 1.0200i Columns 21 through 24: 0.7071 + 0.2929i 0.1710 - 0.1403i 0.0898 + 0.4514i 1.0293 + 0.3122i Columns 25 through 28: 1.0000 - 1.0000i -0.3805 - 1.2542i -0.6756 - 0.1344i 0.2625 - 0.3199i Columns 29 through 32: -0.7071 - 1.7071i -3.3624 - 0.3312i -2.6310 + 3.9375i 2.6719 + 4.9988i CF = Columns 1 through 3: 54.0000 + 0i -21.0721 - 39.4230i -13.2269 + 19.7954i Columns 4 through 6: 5.4603 + 0.5378i 0.7071 + 1.7071i 0.5330 - 0.6494i Columns 7 through 9: 1.0111 + 0.2011i -0.0588 - 0.1938i 1.0000 - 1.0000i Columns 10 through 12: -1.3251 - 0.4020i 0.0600 + 0.3016i 0.0563 - 0.0462i Columns 13 through 15: -0.7071 - 0.2929i 0.0926 + 0.9400i 0.1558 - 0.1041i Columns 16 through 18: 0.3138 + 0.1678i 0 + 0i 0.3138 - 0.1678i Columns 19 through 21: 0.1558 + 0.1041i 0.0926 - 0.9400i -0.7071 + 0.2929i Columns 22 through 24: 0.0563 + 0.0462i 0.0600 - 0.3016i -1.3251 + 0.4020i Columns 25 through 27: 1.0000 + 1.0000i -0.0588 + 0.1938i 1.0111 - 0.2011i Columns 28 through 30: 0.5330 + 0.6494i 0.7071 - 1.7071i 5.4603 - 0.5378i Columns 31 and 32: -13.2269 - 19.7954i -21.0721 + 39.4230i CC = Columns 1 through 8: 0.0000 0.0000 0.0000 0.0000 1.0000 2.0000 3.0000 4.0000 Columns 9 through 16: 5.0000 6.0000 6.0000 6.0000 6.0000 5.0000 4.0000 3.0000 Columns 17 through 24: 2.0000 1.0000 0.0000 0.0000 0.0000 0 0.0000 0.0000 Columns 25 through 32: -0.0000 0.0000 0.0000 0.0000 -0.0000 0.0000 0.0000 0.00002.6.4 向量函数
a=[1 2 3]; b=[4 5 6]; c=cross(a,b) d=dot(a,b) c = -3 6 -3 d = 32