多维阵列
MATLAB®中的多维阵列是一个超过两个维度的阵列。在矩阵中,两个维度由行和列表示。
Each element is defined by two subscripts, the row index and the column index. Multidimensional arrays are an extension of 2-D matrices and use additional subscripts for indexing. A 3-D array, for example, uses three subscripts. The first two are just like a matrix, but the third dimension representspagesor床单of elements.
创建多维阵列
您可以通过首先创建2D矩阵创建多维数组,然后将其扩展。例如,首先将3 x-3矩阵定义为3-D数组中的第一页。
a =[1 2 3; 4 5 6; 7 8 9]
a =3×31 2 3 4 5 6 7 8 9
Now add a second page. To do this, assign another 3-by-3 matrix to the index value 2 in the third dimension. The syntaxA2)
在第一和第二维中使用结肠来包括分配的右侧所有行和所有列。
A2)= [10 11 12; 13 14 15; 16 17 18]
a = a = a(:,:,1)= 1 2 3 4 5 6 7 8 9 a(:,:,:,2)= 10 11 12 13 14 15 16 17 18
这cat
function can be a useful tool for building multidimensional arrays. For example, create a new 3-D arrayb
by concatenating一个
with a third page. The first argument indicates which dimension to concatenate along.
B = CAT(3,A,[3 2 1; 0 9 8; 5 3 7])
b = b(:,:,1)= 1 2 3 4 5 6 7 8 9 b(:,:,:,2)= 10 11 12 13 14 15 16 17 18 b(:,:,:,3)= 3 2 10 9 8 5 3 7
快速扩展多维数组的另一种方法是将单个元素分配给整个页面。例如,将第四页添加到b
其中包含所有零。
b(:,:,4) = 0
b = b(:,:,1)= 1 2 3 4 5 6 7 8 9 b(:,:,:,2)= 10 11 12 13 14 15 16 17 18 b(:,:,:,3)= 3 2 10 9 8 5 3 7b(:,:,4) = 0 0 0 0 0 0 0 0 0
一个ccessing Elements
要在多维数组中访问元素,请像对向量和矩阵一样使用整数下标。例如,查找1,2,2元素一个
,在第一行,第二列和第二页一个
。
一个
a = a = a(:,:,1)= 1 2 3 4 5 6 7 8 9 a(:,:,:,2)= 10 11 12 13 14 15 16 17 18
elA = A(1,2,2)
ELA = 11
使用索引向量[1 3]
在第二个维度访问只有第一nd last columns of each page of一个
。
c = a(:,[1 3],:)
c = c(:,:,1)= 1 3 4 6 7 9 C(:,:,:,2)= 10 12 13 15 16 18
To find the second and third rows of each page, use the colon operator to create your index vector.
d = a(2:3,:,,:)
d = d(:,:,1)= 4 5 6 7 8 9 d(:,:,:,2)= 13 14 15 16 17 18
操纵阵列
多维阵列的元素可以通过多种方式移动,类似于向量和矩阵。重塑
,,,,输入
,,,,and挤
是重新安排元素的有用功能。考虑一个带有两页的3-D阵列。
重塑多维阵列可用于执行某些操作或可视化数据。使用重塑
function to rearrange the elements of the 3-D array into a 6-by-5 matrix.
a = [1 2 3 4 5;9 0 6 3 7;8 1 5 0 2];a(::,:,2)= [9 7 8 5 2;3 5 8 5 1;6 9 4 3 3];b =重塑(a,[6 5])
b=6×51 3 5 7 5 9 6 7 5 5 8 5 2 9 3 2 4 9 8 2 0 3 3 8 1 1 0 6 4 3
重塑
操作柱状,通过将连续元素沿每列的连续元素取下来创建新矩阵一个
,从第一页开始,然后移至第二页。
Permutations are used to rearrange the order of the dimensions of an array. Consider a 3-D arraym
。
m(::,:,1)= [1 2 3;4 5 6;7 8 9];m(::,:,2)= [0 5 4;2 7 6;9 3 1]
m= M(:,:,1) = 1 2 3 4 5 6 7 8 9 M(:,:,2) = 0 5 4 2 7 6 9 3 1
使用输入
通过指定第二个参数中的维度顺序来互换行和列下标。原始行m
are now columns, and the columns are now rows.
p1 = permute(m,[2 1 3])
p1 = p1(:,::,1)= 1 4 7 2 5 8 3 6 9 9 p1(:,:,:,2)= 0 2 9 5 7 3 4 6 1 1
Similarly, interchange row and page subscripts ofm
。
P2 = permute(M,[3 2 1])
P2 = P2(:,:,1) = 1 2 3 0 5 4 P2(:,:,2) = 4 5 6 2 7 6 P2(:,:,3) = 7 8 9 9 3 1
When working with multidimensional arrays, you might encounter one that has an unnecessary dimension of length 1. The挤
函数执行另一种消除长度尺寸的操作类型。例如,使用repmat
函数创建一个2 x-3-by-1 x-4数组的元素,其元素为5,其第三维为1。
a =repmat(5,[2 3 1 4])
a = a = a(:,:,:,1,1)= 5 5 5 5 5 5 a(::,:,:,1,2)= 5 5 5 5 5 5 5 a(::, :, :,:,1,3)= 5 5 5 55 5 5 a(::,:,1,4)= 5 5 5 5 5 5 5
sza = size(a)
sza =1×42 3 1 4
numdimsA = ndims(A)
numdimsA = 4
使用挤
删除第三维的功能,从而产生3D数组。
b =挤压(a)
b = b(:,:,1)= 5 5 5 5 5 5 b(::,::,:,2)= 5 5 5 5 5 5 b(:, :, :,3)= 5 5 5 5 5 5 5 5 b(:,:,4)= 5 5 5 5 5 5
szb = size(b)
SZB =1×32 3 4
numdimsb = ndims(b)
numdimsb = 3