主要内容

reshape

重塑symbolic array

Description

example

重塑(A,n1,n2)returns then1-by-N2matrix, which has the same elements asA。这些元素是从专栏中获取的A填写n1-by-N2matrix.

example

重塑(A,N1,...,NM)returns then1-by-。。。-by-nm数组,其元素与A。这些元素是从专栏中获取的A填写n1-by-。。。-by-nmarray.

example

重塑(A,...,[],...)lets you represent a size value with the placeholder[]while calculating the magnitude of that size value automatically. For example, ifAhas size 2-by-6, then重塑(A,4,[])returns a 4-by-3 array.

example

重塑(A,sz)重塑Ainto an array with size specified bysz, whereszis a vector.

Examples

重塑Symbolic Row Vector into Column Vector

重塑V,这是一个1 x-4行向量,进入4 x-1列向量Y。这里,VYmust have the same number of elements.

Create the vectorV

syms f(x) y V = [3 f(x) -4 y]
V = [ 3, f(x), -4, y]

重塑VintoY

Y = reshape(V,4,1)
y = 3 f(x)-4 y

Alternatively, usey =V。'where。'is the nonconjugate transpose.

重塑Symbolic Matrix

重塑the 2-by-6 symbolic matrixMinto a 4-by-3 matrix.

m = sym([1 9 4 3 0 1; 3 9 5 1 9 2])N = reshape(M,4,3)
m = [1,9,4,3,0,1] [3,9,5,1,9,2] n = [1,4,0] [3,5,9]] [9,1,2]

MNmust have the same number of elements.reshape阅读Mcolumn-wise to fill in the elements ofNcolumn-wise.

Alternatively, use a size vector to specify the dimensions of the reshaped matrix.

sz = [4 3]; N = reshape(M,sz)
n = [1,4,0] [3,5,9] [9,3,1] [9,1,2]

Automatically Set Dimension of Reshaped Matrix

When you replace a dimension with the placeholder[],reshape计算该维度的所需大小以重塑矩阵。

创建矩阵M

m = sym([1 9 4 3 0 1; 3 9 5 1 9 2])
m = [1、9、4、3、0、1] [3、9、5、1、9、2]

重塑Minto a matrix with three columns.

重塑(M,[],3)
ans = [1,4,0] [3,5,9] [9,3,1] [9,1,2]

reshape计算出三列的重塑矩阵需要四行。

重塑矩阵行

重塑a matrix row-wise by transposing the result.

创建矩阵M

syms x m = sym([1 9 0 sin(x)2 2; nan x 5 1 4 7])))
M = [ 1, 9, 0, sin(x), 2, 2] [ NaN, x, 5, 1, 4, 7]

重塑M通过转移结果来行。

重塑(M,4,3).'
ans = [ 1, NaN, 9, x] [ 0, 5, sin(x), 1] [ 2, 4, 2, 7]

Note that。'返回非偶联的转置'返回共轭转置。

重塑3-D Array into 2-D Matrix

重塑the 3-by-3-by-2 arrayMinto a 9-by-2 matrix.

Mhas 18 elements. Because a 9-by-2 matrix also has 18 elements,Mcan be reshaped into it. ConstructM

syms x M = [sin(x) x 4; 3 2 9; 8 x x]; M(:,:,2) = M'
M(:,:,1) = [ sin(x), x, 4] [ 3, 2, 9] [ 8, x, x] M(:,:,2) = [ sin(conj(x)), 3, 8] [ conj(x), 2, conj(x)] [ 4, 9, conj(x)]

重塑Minto a 9-by-2 matrix.

n =重塑(M,9,2)
N = [ sin(x), sin(conj(x))] [ 3, conj(x)] [ 8, 4] [ x, 3] [ 2, 2] [ x, 9] [ 4, 8] [ 9, conj(x)] [ x, conj(x)]

使用重塑分解阵列

Usereshapeinstead of loops to break up arrays for further computation. Usereshape分解矢量Vto find the product of every three elements.

Create vectorV

syms x V = [exp(x) 1 3 9 x 2 7 7 1 8 x^2 3 4 sin(x) x]
V = [ exp(x), 1, 3, 9, x, 2, 7, 7, 1, 8, x^2, 3, 4, sin(x), x]

Specify3for the number of rows. Use the placeholder[]对于列数。这让reshape自动计算三行所需的列数。

M = prod( reshape(V,3,[]) )
M = [ 3*exp(x), 18*x, 49, 24*x^2, 4*x*sin(x)]

reshapecalculates that five columns are required for a matrix of three rows.prodthen multiples the elements of each column to return the result.

Input Arguments

collapse all

输入阵列,指定为符号向量,矩阵或多维数组。

Dimensions of reshaped matrix, specified as comma-separated scalars. For example,重塑(A,3,2)returns a 3-by-2 matrix. The number of elements in the output array specified byn1,n2must be equal tonumel(a)

Dimensions of reshaped array, specified as comma-separated scalars. For example,重塑(A,3,2,2)returns a 3-by-2-by-2 matrix. The number of elements in the output array specified byN1,...,NMmust be equal tonumel(a)

Size of reshaped array, specified as a numeric vector. For example,重塑(A,[3 2])returns a 3-by-2 matrix. The number of elements in the output array specified byszmust be equal tonumel(a)

版本历史

在R2006a之前引入

See Also

|