1、第二章 数值数组及其运算数值数组(Numeric Array)和数组运算(Array Operations)始终是MATLAB的核心内容。自MATLAB5.x版起,由于其“面向对象”的特征,这种数值数组(以下简称为数组)成为了MATALB最重要的一种内建数据类型(Built-in Data Type),而数组运算就是定义在这种数据结构上的方法(Method)。本章系统阐述:一、二维数值数组的创建、寻访;数组运算和矩阵运算的区别;实现数组运算的基本函数;多项式的表达、创建和操作;常用标准数组生成函数和数组构作技法;高维数组的创建、寻访和操作;非数NaN、“空”数组概念和应用;关系和逻辑操作。顺便
2、指出:(1)本章所涉内容和方法,不仅使用于数值数组,而且也将部分地延伸使用于在其他数据结构中。(2)MATLAB5.x和6.x 版在本章内容上的差异极微。(3)MATLAB6.5版新增的两种逻辑操作,在第2.13.2节给予介绍。2.1 引导【例2.1-1】绘制函数在时的曲线。x=0:0.1:1y=x.*exp(-x)plot(x,y),xlabel(x),ylabel(y),title(y=x*exp(-x) x = Columns 1 through 7 0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 Columns 8 through 11 0.70
3、00 0.8000 0.9000 1.0000y = Columns 1 through 7 0 0.0905 0.1637 0.2222 0.2681 0.3033 0.3293 Columns 8 through 11 0.3476 0.3595 0.3659 0.3679图2.1-12.2 一维数组的创建和寻访2.2.1 一维数组的创建2.2.2 一维数组的子数组寻访和赋值【例2.2.2-1】子数组的寻访(Address)。rand(state,0) x=rand(1,5) x =0.9501 0.2311 0.6068 0.4860 0.8913 x(3) ans =0.6068 x(
4、1 2 5) ans =0.9501 0.2311 0.8913 x(1:3) ans = 0.9501 0.2311 0.6068 x(3:end)% ans = 0.6068 0.4860 0.8913 x(3:-1:1)% ans = 0.6068 0.2311 0.9501 x(find(x0.5) ans =0.9501 0.6068 0.8913 x(1 2 3 4 4 3 2 1) ans = Columns 1 through 7 0.9501 0.2311 0.6068 0.4860 0.4860 0.6068 0.2311 Column 8 0.9501 【例2.2.2-2
5、】子数组的赋值(Assign)。x(3) = 0 x = 0.9501 0.2311 0 0.4860 0.8913 x(1 4)=1 1 x = 1.0000 0.2311 0 1.0000 0.8913 2.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.0000 5.4716 + 0.4177i 0.6909 0.7071 4.8244 3.5000 + 1.0000i 【例2.3.1-2】
6、复数数组的另一种输入方式。M_r=1,2,3;4,5,6,M_i=11,12,13;14,15,16CN=M_r+i*M_i M_r = 1 2 3 4 5 6M_i = 11 12 13 14 15 16CN = 1.0000 +11.0000i 2.0000 +12.0000i 3.0000 +13.0000i 4.0000 +14.0000i 5.0000 +15.0000i 6.0000 +16.0000i 2.3.2 利用M文件创建和保存数组【例2.3.2-1】创建和保存数组 AM的 MyMatrix.m 文件。(1)% MyMatrix.mCreation and preserva
7、tion of matrix AMAM=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】找出数组中所有绝对值大于3的元素。A=zeros(2,5);A(:)=-4:5L=abs(A)3islogical(L)X=A(L) A = -4 -2 0 2 4 -3 -1 1 3 5L = 1
8、0 0 0 1 0 0 0 0 1ans = 1X = -4 4 5 【例2.4.3-2】演示逻辑数组与一般双精度数值数组的关系和区别。(本例在例2.4.3-1基础上进行)。(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 = 1 1 1 1 1 1 1 1 1 1c_N =doublec_L =double (2)islogical(Num)Y=A(Num) ans = 0? Index into matrix is negative or zero. See release notes on changes
9、 to logical indices. 2.5 二维数组的子数组寻访和赋值【例2.5-1】不同赋值方式示例。A=zeros(2,4) A = 0 0 0 0 0 0 0 0 A(:)=1:8 A = 1 3 5 7 2 4 6 8 s=2 3 5;A(s)Sa=10 20 30A(s)=Sa ans = 2 3 5Sa = 10 20 30A = 1 20 30 7 10 4 6 8 A(:,2 3)=ones(2) A = 1 1 1 7 10 1 1 8 2.6 执行数组运算的常用函数2.6.1 函数数组运算规则的定义:2.6.2 执行数组运算的常用函数【例2.6.2-1】演示pow2的
10、数组运算性质。A=1:4;5:8 A = 1 2 3 4 5 6 7 8 pow2(A) ans = 2 4 8 16 32 64 128 256 2.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.0000i 3.0000 + 3.0000i 5.0000 + 5.0000i 2.0000 + 2.0000i 4.0000 + 4.0000i 6.0000 + 6.0000iA_A = 1.0000 + 1.0
11、000i 2.0000 + 2.0000i 3.0000 + 3.0000i 4.0000 + 4.0000i 5.0000 + 5.0000i 6.0000 + 6.0000iA_M = 1.0000 - 1.0000i 2.0000 - 2.0000i 3.0000 - 3.0000i 4.0000 - 4.0000i 5.0000 - 5.0000i 6.0000 - 6.0000i 2.8 多项式的表达方式及其操作2.8.1 多项式的表达和创建 一 多项式表达方式的约定 二 多项式行向量的创建方法【例 2.8.1.2-1】求3阶方阵A的特征多项式。A=11 12 13;14 15 16
12、;17 18 19;PA=poly(A) PPA=poly2str(PA,s) PA = 1.0000 -45.0000 -18.0000 0.0000PPA = s3 - 45 s2 - 18 s + 1.8303e-014 【例 2.8.1.2-2】由给定根向量求多项式系数向量。R=-0.5,-0.3+0.4*i,-0.3-0.4*i;P=poly(R)PR=real(P) PPR=poly2str(PR,x) P = 1.0000 1.1000 0.5500 0.1250PR = 1.0000 1.1000 0.5500 0.1250PPR = x3 + 1.1 x2 + 0.55 x
13、+ 0.125 2.8.2 多项式运算函数【例2.8.2-1】求的“商”及“余”多项式。p1=conv(1,0,2,conv(1,4,1,1);p2=1 0 1 1;q,r=deconv(p1,p2);cq=商多项式为 ; cr=余多项式为 ;disp(cq,poly2str(q,s),disp(cr,poly2str(r,s) 商多项式为 s + 5余多项式为 5 s2 + 4 s + 3 【例 2.8.2-2】两种多项式求值指令的差别。S=pascal(4)P=poly(S);PP=poly2str(P,s)PA=polyval(P,S)PM=polyvalm(P,S) S = 1 1 1
14、 1 1 2 3 4 1 3 6 10 1 4 10 20PP = s4 - 29 s3 + 72 s2 - 29 s + 1PA = 1.0e+004 * 0.0016 0.0016 0.0016 0.0016 0.0016 0.0015 -0.0140 -0.0563 0.0016 -0.0140 -0.2549 -1.2089 0.0016 -0.0563 -1.2089 -4.3779PM = 1.0e-010 * 0.0016 0.0033 0.0090 0.0205 0.0045 0.0101 0.0286 0.0697 0.0095 0.0210 0.0653 0.1596 0.
15、0163 0.0387 0.1226 0.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.1513i 1.1274 - 1.1513i -0.0232 - 0.0722i -0.0232 + 0.0722i 0.7916 s = -1.7680 + 1.2673i -1.7680 - 1.2673i 0.4176 + 1.1130i 0.4176 - 1.1130i -0.2991 k = 2.9 标准数组生成函数和数组操作函数2.9.1 标准数组生成函数【例2.9.1-1】标
16、准数组产生的演示。ones(1,2) ans = 1 1 ones(2) ans = 1 1 1 1 randn(state,0)randn(2,3) ans = -0.4326 0.1253 -1.1465 -1.6656 0.2877 1.1909 D=eye(3) D = 1 0 0 0 1 0 0 0 1 diag(D) ans = 1 1 1 diag(diag(D) ans = 1 0 0 0 1 0 0 0 1 repmat(D,1,3) ans = Columns 1 through 8 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0
17、 0 Column 9 0 01 2.9.2 数组操作函数【例 2.9.2-1】diag与reshape的使用演示。a=-4:4A=reshape(a,3,3) a = Columns 1 through 8 -4 -3 -2 -1 0 1 2 3 Column 9 4A = -4 -1 2 -3 0 3 -2 1 4 a1=diag(A,1) a1 = -1 3 A1=diag(a1,-1) A1 = 0 0 0 -1 0 0 0 3 0 【例2.9.2-2】数组转置、对称交换和旋转操作后果的对照比较。A A = -4 -1 2 -3 0 3 -2 1 4 A. ans = -4 -3 -2
18、 -1 0 1 2 3 4 flipud(A) ans = -2 1 4 -3 0 3 -4 -1 2 fliplr(A) ans = 2 -1 -4 3 0 -3 4 1 -2 rot90(A) ans = 2 3 4 -1 0 1 -4 -3 -2 【例2.9.2-3】演示Kronecker乘法不具备“可交换规律”。B=eye(2)C=reshape(1:4,2,2) B = 1 0 0 1C = 1 3 2 4 kron(B,C) ans = 1 3 0 0 2 4 0 0 0 0 1 3 0 0 2 4 kron(C,B) ans = 1 0 3 0 0 1 0 3 2 0 4 0 0
19、 2 0 4 2.10 数组构作技法综合【例2.10-1】数组的扩展。(1)数组的赋值扩展法A=reshape(1:9,3,3) A = 1 4 7 2 5 8 3 6 9 A(5,5)=111 A = 1 4 7 0 0 2 5 8 0 0 3 6 9 0 0 0 0 0 0 0 0 0 0 0 111 A(:,6)=222 A = 1 4 7 0 0 222 2 5 8 0 0 222 3 6 9 0 0 222 0 0 0 0 0 222 0 0 0 0 111 222 (2)多次寻访扩展法AA=A(:,1:6,1:6) AA = 1 4 7 0 0 222 1 4 7 0 0 222
20、2 5 8 0 0 222 2 5 8 0 0 222 3 6 9 0 0 222 3 6 9 0 0 222 0 0 0 0 0 222 0 0 0 0 0 222 0 0 0 0 111 222 0 0 0 0 111 222 (3)合成扩展法B=ones(2,6) B = 1 1 1 1 1 1 1 1 1 1 1 1 AB_r=A;B AB_r = 1 4 7 0 0 222 2 5 8 0 0 222 3 6 9 0 0 222 0 0 0 0 0 222 0 0 0 0 111 222 1 1 1 1 1 1 1 1 1 1 1 1 AB_c=A,B(:,1:5) AB_c = 1
21、 4 7 0 0 222 1 1 2 5 8 0 0 222 1 1 3 6 9 0 0 222 1 1 0 0 0 0 0 222 1 1 0 0 0 0 111 222 1 1 【例2.10-2】提取子数组,合成新数组。A A = 1 4 7 0 0 222 2 5 8 0 0 222 3 6 9 0 0 222 0 0 0 0 0 222 0 0 0 0 111 222 AB_BA=triu(A,1)+tril(A,-1) AB_BA = 0 4 7 0 0 222 2 0 8 0 0 222 3 6 0 0 0 222 0 0 0 0 0 222 0 0 0 0 0 222 AB1=A
22、(1:2,end:-1:1);B(1,:) AB1 = 222 0 0 7 4 1 222 0 0 8 5 2 1 1 1 1 1 1 【例2.10-3】单下标寻访和reshape指令演示。clearA=reshape(1:16,2,8) A = 1 3 5 7 9 11 13 15 2 4 6 8 10 12 14 16 reshape(A,4,4) ans = 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16 s=1 3 6 8 9 11 14 16;A(s)=0 A = 0 0 5 7 0 0 13 15 2 4 0 0 10 12 0 0 【例2.10-4】
23、“对列(或行)同加一个数”三种的操作方法。clear,A=reshape(1:9,3,3) A = 1 4 7 2 5 8 3 6 9 b=1 2 3;A_b1=A-b(1 1 1,:) A_b1 = 0 2 4 1 3 5 2 4 6 A_b2=A-repmat(b,3,1) A_b2 = 0 2 4 1 3 5 2 4 6 A_b3=A(:,1)-b(1),A(:,2)-b(2),A(:,3)-b(3) A_b3 = 0 2 4 1 3 5 2 4 6 【例2.10-5】逻辑函数的运用示例。randn(state,1),R=randn(3,6) R = 0.8644 0.8735 -1.1
24、027 0.1684 -0.5523 -0.6149 0.0942 -0.4380 0.3962 -1.9654 -0.8197 -0.2546 -0.8519 -0.4297 -0.9649 -0.7443 1.1091 -0.2698 L=abs(R)1.5 L = 0 0 0 1 0 0 1 1 1 1 0 1 0 1 0 0 0 1 R(L)=0 R = 0.8644 0.8735 -1.1027 0 -0.5523 -0.6149 0 0 0 0 -0.8197 0 -0.8519 0 -0.9649 -0.7443 1.1091 0 s=(find(R=0) s = 2 5 6 8
25、 10 11 17 18 R(s)=111 R = 0.8644 0.8735 -1.1027 111.0000 -0.5523 -0.6149 111.0000 111.0000 111.0000 111.0000 -0.8197 111.0000 -0.8519 111.0000 -0.9649 -0.7443 1.1091 111.0000 ii,jj=find(R=111);disp(ii),disp(jj) 2 2 3 2 1 2 2 31 2 2 3 4 4 6 6 2.11 高维数组2.11.1 高维数组的创建【例2.11.1-1】“全下标”元素赋值方式创建高维数组演示。A(2,
26、2,2)=1 A(:,:,1) =0 00 0A(:,:,2) =0 0 0 1 B(2,5,:)=1:3 B(:,:,1) = 0 0 0 0 0 0 0 0 0 1B(:,:,2) = 0 0 0 0 0 0 0 0 0 2B(:,:,3) = 0 0 0 0 0 0 0 0 0 3 【例2.11.1-2】低维数组合成高维数组。clear,A=ones(2,3);A(:,:,2)=ones(2,3)*2;A(:,:,3)=ones(2,3)*3 A(:,:,1) = 1 1 1 1 1 1A(:,:,2) = 2 2 2 2 2 2A(:,:,3) = 3 3 3 3 3 3 【例2.11
27、.1-3】由函数ones, zeros, rand, randn直接创建标准高维数组的示例。rand(state,1111),rand(2,4,3) ans(:,:,1) = 0.6278 0.9748 0.2585 0.6949 0.2544 0.2305 0.0313 0.1223ans(:,:,2) = 0.4889 0.3898 0.8489 0.0587 0.9138 0.3071 0.4260 0.6331ans(:,:,3) = 0.2802 0.2073 0.7438 0.2714 0.4051 0.2033 0.4566 0.2421 【例2.11.1-4】借助cat, re
28、pmat, reshape等函数构作高维数组。(1)cat(3,ones(2,3),ones(2,3)*2,ones(2,3)*3) ans(:,:,1) = 1 1 1 1 1 1ans(:,:,2) = 2 2 2 2 2 2ans(:,:,3) = 3 3 3 3 3 3 (2)repmat(ones(2,3),1,1,3) ans(:,:,1) = 1 1 1 1 1 1ans(:,:,2) = 1 1 1 1 1 1ans(:,:,3) = 1 1 1 1 1 1 (3)reshape(1:12,2,2,3) ans(:,:,1) = 1 3 2 4ans(:,:,2) = 5 7
29、6 8ans(:,:,3) = 9 11 10 12 2.11.2 高维数组的标识【例2.11.2-1】维数、大小和长度clear;A=reshape(1:24,2,3,4);dim_A=ndims(A)size_A=size(A)L_A=length(A) dim_A = 3size_A = 2 3 4L_A =4 2.11.3 高维数组构作和操作函数汇总【例2.11.3-1】数组元素对称交换指令flipdim的使用示例。A=reshape(1:18,2,3,3) A(:,:,1) = 1 3 5 2 4 6A(:,:,2) = 7 9 11 8 10 12A(:,:,3) = 13 15
30、17 14 16 18 flipdim(A,1) ans(:,:,1) = 2 4 6 1 3 5ans(:,:,2) = 8 10 12 7 9 11ans(:,:,3) = 14 16 18 13 15 17 flipdim(A,3) ans(:,:,1) = 13 15 17 14 16 18ans(:,:,2) = 7 9 11 8 10 12ans(:,:,3) = 1 3 5 2 4 6 【例2.11.3-2】数组的“维序号左移”重组。shiftdim(A,1) ans(:,:,1) = 1 7 13 3 9 15 5 11 17ans(:,:,2) = 2 8 14 4 10 16 6 12 18 shiftdim(A,2) ans(:,:,1) = 1 2 7