repmat(ones(2,3),[1,1,3]) 1,11 ans(:;:,3) =11=11=11 ans(:;:,2)= reshape(1:12,2,2,3) =34=7 ans( 12,56,9 2.11.2 高维数组的标识 【例2.112-1】维数、大小和长度 clear:Reshape(1: 24,2,3,4)i dim Andims(A) L A=length(A) dim a= 3A2 2113高维数组构作和操作函数汇总 【例2113-1】数组元素对称交换指令 flipdim的使用示例。 A= reshape(1:18,2,3,3) A(: 12,7 13 14 flipdim(A, 1) ans 21,87 12
13 repmat(ones(2,3),[1,1,3]) ans(:,:,1) = 1 1 1 1 1 1 ans(:,:,2) = 1 1 1 1 1 1 ans(:,:,3) = 1 1 1 1 1 1 (3) reshape(1:12,2,2,3) ans(:,:,1) = 1 3 2 4 ans(:,:,2) = 5 7 6 8 ans(:,:,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 = 3 size_A = 2 3 4 L_A = 4 2.11.3 高维数组构作和操作函数汇总 【例 2.11.3-1】数组元素对称交换指令 flipdim 的使用示例。 A=reshape(1:18,2,3,3) A(:,:,1) = 1 3 5 2 4 6 A(:,:,2) = 7 9 11 8 10 12 A(:,:,3) = 13 15 17 14 16 18 flipdim(A,1) ans(:,:,1) = 2 4 6 1 3 5 ans(:,:,2) = 8 10 12 7 9 11
ans flipdim(A, 3) ans ans 34,78,12 11 12 ans(:;:,3)= 【例2.113-2】数组的“维序号左移”重组 shiftdim(A, 1) ans 7 135,24 ans(:;:;2)= 16 12 18 shiftdim(A, 2) ans(:;:;1)= ans 173,39 8y 06=6 11 17 【例2.11.3-3】广义非共轭转置。 permute(A,[2,3, 11) ans(:;:;1)= 13 ans 135,246 =802 1 468 permute(A,[1,3, 21) ans 14 ans(:,: 34,56 8=90= ans
14 ans(:,:,3) = 14 16 18 13 15 17 flipdim(A,3) ans(:,:,1) = 13 15 17 14 16 18 ans(:,:,2) = 7 9 11 8 10 12 ans(:,:,3) = 1 3 5 2 4 6 【例 2.11.3-2】数组的“维序号左移”重组。 shiftdim(A,1) ans(:,:,1) = 1 7 13 3 9 15 5 11 17 ans(:,:,2) = 2 8 14 4 10 16 6 12 18 shiftdim(A,2) ans(:,:,1) = 1 2 7 8 13 14 ans(:,:,2) = 3 4 9 10 15 16 ans(:,:,3) = 5 6 11 12 17 18 【例 2.11.3-3】广义非共轭转置。 permute(A,[2,3,1]) ans(:,:,1) = 1 7 13 3 9 15 5 11 17 ans(:,:,2) = 2 8 14 4 10 16 6 12 18 permute(A,[1,3,2]) ans(:,:,1) = 1 7 13 2 8 14 ans(:,:,2) = 3 9 15 4 10 16 ans(:,:,3) = 5 11 17 6 12 18
【例2.11.3-4】“孤维”的撤消和降维 B=cat(4,A(:;:,1),A(:;:,2),A(:;:,3)) =34 1 78,3 12 B( 1,3) size(B) C=squeeze(B) c(:;:;1) 2 C( 2) 3)= 18 s主ze(C) ans 【例2.113-5】赋“空阵”值操作。 A= reshape(1:182,3,3) A( 5 78 11 12 A(:;:,3) 13 517 14 16 18 A(:2:3,:)=[ B=Ai A( 12,78,3 s主ze(A) ns= 1 =squeeze(A)
15 【例 2.11.3-4】“孤维”的撤消和降维。 B=cat(4,A(:,:,1),A(:,:,2),A(:,:,3)) B(:,:,1,1) = 1 3 5 2 4 6 B(:,:,1,2) = 7 9 11 8 10 12 B(:,:,1,3) = 13 15 17 14 16 18 size(B) ans = 2 3 1 3 C=squeeze(B) C(:,:,1) = 1 3 5 2 4 6 C(:,:,2) = 7 9 11 8 10 12 C(:,:,3) = 13 15 17 14 16 18 size(C) ans = 2 3 3 【例 2.11.3-5】赋“空阵”值操作。 A=reshape(1:18,2,3,3) A(:,:,1) = 1 3 5 2 4 6 A(:,:,2) = 7 9 11 8 10 12 A(:,:,3) = 13 15 17 14 16 18 A(:,2:3,:)=[] B=A; A(:,:,1) = 1 2 A(:,:,2) = 7 8 A(:,:,3) = 13 14 size(A) ans = 2 1 3 A_1=squeeze(A)
s主ze(B) ans= B(:,1,:)=[ Empty array: 2-by-0-by-3 212“非数”和“空”数组 2.12.1 非数NaN 【例2.12.1-1】非数的产生和性质演示。 (1) =0/0,b=0*1og(0),c=inf-inf Warning: Divide by zero Warning: Log of zero NaN NaN (2) o*a, sin(a) NaN 3) aanan 0 (4) anan a=b b>c ans ans ans 100 class(a) isnan(a)
16 A_1 = 1 7 13 2 8 14 size(B) ans = 2 1 3 B(:,1,:)=[] B = Empty array: 2-by-0-by-3 2.12 “非数”和“空”数组 2.12.1 非数 NaN 【例 2.12.1-1】非数的产生和性质演示。 (1) a=0/0,b=0*log(0),c=inf-inf Warning: Divide by zero. a = NaN Warning: Log of zero. b = NaN c = NaN (2) 0*a,sin(a) ans = NaN ans = NaN (3) a==nan ans = 0 (4) a~=nan a==b b>c ans = 1 ans = 0 ans = 0 (5) class(a) isnan(a) ans = double ans = 1
【例2.12.1-2】非数元素的寻访 rand('state,o) R=rand(2,5);R(1,5)=NaN;R(2,3)=NaN 0.9501 0.6068 0.8913 0.4565 NaN 0.2311 0.4860 NaN 0.01850.4447 ans 0 1 Linear index=find (isnan(R)) [r index, c index]=ind2sub(size(r), Linear index)i disp(r index c index '),disp([r index c index]) Linear index r index c index 2.12.2 “空”数组 【例2.12.2-1】关于“空”数组的算例 (1) a=[],b=ones(2,0),c= zeros(2,0),d=eye(2,0),f=rand(2,3,0,4) Empty mat 2-by-0 Empty matrix: 2-by-O Empty matrix: 2-by Empty array: 2-by-3-by-0-by-4 class(a) isnumeric(a) isempty(a) ans double ans ans which a dims(a) size(a) a is a variable ans
17 【例 2.12.1-2】非数元素的寻访 rand('state',0) R=rand(2,5);R(1,5)=NaN;R(2,3)=NaN R = 0.9501 0.6068 0.8913 0.4565 NaN 0.2311 0.4860 NaN 0.0185 0.4447 isnan(R) ans = 0 0 0 0 1 0 0 1 0 0 Linear_index=find(isnan(R)) [r_index,c_index]=ind2sub(size(R),Linear_index); disp('r_index c_index'),disp([r_index c_index]) Linear_index = 6 9 r_index c_index 2 3 1 5 2.12.2 “空”数组 【例 2.12.2-1】关于“空”数组的算例。 (1) a=[],b=ones(2,0),c=zeros(2,0),d=eye(2,0),f=rand(2,3,0,4) a = [] b = Empty matrix: 2-by-0 c = Empty matrix: 2-by-0 d = Empty matrix: 2-by-0 f = Empty array: 2-by-3-by-0-by-4 (2) class(a) isnumeric(a) isempty(a) ans = double ans = 1 ans = 1 which a ndims(a) size(a) a is a variable. ans = 2 ans = 0 0