1第二章数值数组及其运算数值数组(NumericArray)和数组运算(ArrayOperations)始终是MATLAB的核心内容。自MATLAB5.x版起,由于其“面向对象”的特征,这种数值数组(以下简称为数组)成为了MATALB最重要的一种内建数据类型(Built-inDataType),而数组运算就是定义在这种数据结构上的方法(Method)。本章系统阐述:一、二维数值数组的创建、寻访;数组运算和矩阵运算的区别;实现数组运算的基本函数;多项式的表达、创建和操作;常用标准数组生成函数和数组构作技法;高维数组的创建、寻访和操作;非数NaN、“空”数组概念和应用;关系和逻辑操作。顺便指出:(1)本章所涉内容和方法,不仅使用于数值数组,而且也将部分地延伸使用于在其他数据结构中。(2)MATLAB5.x和6.x版在本章内容上的差异极微。(3)MATLAB6.5版新增的两种逻辑操作,在第2.13.2节给予介绍。2.1引导【例2.1-1】绘制函数xxey在10x时的曲线。x=0:0.1:1y=x.*exp(-x)plot(x,y),xlabel('x'),ylabel('y'),title('y=x*exp(-x)')x=Columns1through700.10000.20000.30000.40000.50000.6000Columns8through110.70000.80000.90001.0000y=Columns1through700.09050.16370.22220.26810.30330.3293Columns8through110.34760.35950.36590.367900.20.40.60.8100.050.10.150.20.250.30.350.4xyy=x*exp(-x)图2.1-12.2一维数组的创建和寻访2.2.1一维数组的创建2.2.2一维数组的子数组寻访和赋值【例2.2.2-1】子数组的寻访(Address)。2rand('state',0)x=rand(1,5)x=0.95010.23110.60680.48600.8913x(3)ans=0.6068x([125])ans=0.95010.23110.8913x(1:3)ans=0.95010.23110.6068x(3:end)%ans=0.60680.48600.8913x(3:-1:1)%ans=0.60680.23110.9501x(find(x0.5))ans=0.95010.60680.8913x([12344321])ans=Columns1through70.95010.23110.60680.48600.48600.60680.2311Column80.9501【例2.2.2-2】子数组的赋值(Assign)。x(3)=0x=0.95010.231100.48600.8913x([14])=[11]x=1.00000.231101.00000.89132.3二维数组的创建2.3.1直接输入法【例2.3.1-1】在MATLAB环境下,用下面三条指令创建二维数组C。a=2.7358;b=33/79;C=[1,2*a+i*b,b*sqrt(a);sin(pi/4),a+5*b,3.5+i]C=1.00005.4716+0.4177i0.69090.70714.82443.5000+1.0000i【例2.3.1-2】复数数组的另一种输入方式。3M_r=[1,2,3;4,5,6],M_i=[11,12,13;14,15,16]CN=M_r+i*M_iM_r=123456M_i=111213141516CN=1.0000+11.0000i2.0000+12.0000i3.0000+13.0000i4.0000+14.0000i5.0000+15.0000i6.0000+16.0000i2.3.2利用M文件创建和保存数组【例2.3.2-1】创建和保存数组AM的MyMatrix.m文件。(1)%MyMatrix.mCreationandpreservationofmatrixAMAM=[101,102,103,104,105,106,107,108,109;...201,202,203,204,205,206,207,208,209;...301,302,303,304,305,306,307,308,309];(2)(3)2.4二维数组元素的标识2.4.1“全下标”标识2.4.2“单下标”标识2.4.3“逻辑1”标识【例2.4.3-1】找出数组5311342024A中所有绝对值大于3的元素。A=zeros(2,5);A(:)=-4:5L=abs(A)3islogical(L)X=A(L)A=-4-2024-3-1135L=1000100001ans=1X=-445【例2.4.3-2】演示逻辑数组与一般双精度数值数组的关系和区别。(本例在例2.4.3-1基础上进行)。4(1)Num=[1,0,0,0,1;0,0,0,0,1];N_L=Num==Lc_N=class(Num)c_L=class(L)N_L=1111111111c_N=doublec_L=double(2)islogical(Num)Y=A(Num)ans=0???Indexintomatrixisnegativeorzero.Seereleasenotesonchangestologicalindices.2.5二维数组的子数组寻访和赋值【例2.5-1】不同赋值方式示例。A=zeros(2,4)A=00000000A(:)=1:8A=13572468s=[235];A(s)Sa=[102030]'A(s)=Saans=235Sa=102030A=12030710468A(:,[23])=ones(2)A=1117101182.6执行数组运算的常用函数52.6.1函数数组运算规则的定义:2.6.2执行数组运算的常用函数【例2.6.2-1】演示pow2的数组运算性质。A=[1:4;5:8]A=12345678pow2(A)ans=2481632641282562.7数组运算和矩阵运算2.7.1数组运算和矩阵运算指令对照汇总【例2.7.1-1】两种不同转置的比较clear;A=zeros(2,3);A(:)=1:6;A=A*(1+i)A_A=A.'A_M=A'A=1.0000+1.0000i3.0000+3.0000i5.0000+5.0000i2.0000+2.0000i4.0000+4.0000i6.0000+6.0000iA_A=1.0000+1.0000i2.0000+2.0000i3.0000+3.0000i4.0000+4.0000i5.0000+5.0000i6.0000+6.0000iA_M=1.0000-1.0000i2.0000-2.0000i3.0000-3.0000i4.0000-4.0000i5.0000-5.0000i6.0000-6.0000i2.8多项式的表达方式及其操作2.8.1多项式的表达和创建一多项式表达方式的约定二多项式行向量的创建方法【例2.8.1.2-1】求3阶方阵A的特征多项式。A=[111213;141516;171819];PA=poly(A)PPA=poly2str(PA,'s')PA=1.0000-45.0000-18.00000.0000PPA=s^3-45s^2-18s+1.8303e-014【例2.8.1.2-2】由给定根向量求多项式系数向量。R=[-0.5,-0.3+0.4*i,-0.3-0.4*i];P=poly(R)6PR=real(P)PPR=poly2str(PR,'x')P=1.00001.10000.55000.1250PR=1.00001.10000.55000.1250PPR=x^3+1.1x^2+0.55x+0.1252.8.2多项式运算函数【例2.8.2-1】求1)1)(4)(2(32sssss的“商”及“余”多项式。p1=conv([1,0,2],conv([1,4],[1,1]));p2=[1011];[q,r]=deconv(p1,p2);cq='商多项式为';cr='余多项式为';disp([cq,poly2str(q,'s')]),disp([cr,poly2str(r,'s')])商多项式为s+5余多项式为5s^2+4s+3【例2.8.2-2】两种多项式求值指令的差别。S=pascal(4)P=poly(S);PP=poly2str(P,'s')PA=polyval(P,S)PM=polyvalm(P,S)S=1111123413610141020PP=s^4-29s^3+72s^2-29s+1PA=1.0e+004*0.00160.00160.00160.00160.00160.0015-0.0140-0.05630.0016-0.0140-0.2549-1.20890.0016-0.0563-1.2089-4.3779PM=1.0e-010*0.00160.00330.00900.02050.00450.01010.02860.06970.00950.02100.06530.15960.01630.03870.12260.3019【例2.8.2-3】部分分式展开。a=[1,3,4,2,7,2];b=[3,2,5,4,6];[r,s,k]=residue(b,a)r=1.1274+1.1513i1.1274-1.1513i-0.0232-0.0722i-0.0232+0.0722i0.7916s=-1.7680+1.2673i-1.7680-1.2673i70.4176+1.1130i0.4176-1.1130i-0.2991k=[]2.9标准数组生成函数和数组操作函数2.9.1标准数组生成函数【例2.9.1-1】标准数组产生的演示。ones(1,2)ans=11ones(2)ans=1111randn('state',0)randn(2,3)ans=-0.43260.1253-1.1465-1.66560.28771.1909D=eye(3)D=100010001diag(D)ans=111diag(diag(D))ans=100010001repmat(D,1,3)ans=Columns1through8100100100100100100100100Column90012.9.2数组操作函数【例2.9.2-1】diag与reshape的使用演示。a=-4:48A=reshape(a,3,3)a=Columns1through8-4-3-2-10123Column94A=-4-12-303-214a1=diag(A,1)a1=-13A1=diag(a1,-1)A1=000-100030【例2.9.2-2】数组转置、对称交换和旋转操作后果的对照比较。AA=-4-12-303-214A.'ans=-4-3-2-101234flipud(A)ans=-214-303-4-12fliplr(A)ans=2-1-430-341-2rot90(A)ans=234-101-4-3-2【例2.9.2-3】演示Kronecker乘法不具备“可交换规律”。B=eye(2)C=reshape(1:4,2,2)B=1001C=13249kron(B,C)ans=1300240000130024kron(C,B)ans=10300103204002042.10数组构作技法综合【例2.10-1】数组的扩展。(1)数组的赋值扩展法A=reshape(1:9,3,3)A=147258369A(5,5)=111A=147002580036900000000000111A(:,6)=222A=147002222580022236900222000002220000111222(2)多次寻访扩展法AA=A(:,[1:6,1:6])AA=1470022214