reshape
重塑symbolic array
Description
重塑(
lets you represent a size value with the placeholderA
,...,[],...)[]
while calculating the magnitude of that size value automatically. For example, ifA
has size 2-by-6, then重塑(A,4,[])
returns a 4-by-3 array.
Examples
重塑Symbolic Row Vector into Column Vector
重塑V
,这是一个1 x-4行向量,进入4 x-1列向量Y
。这里,V
和Y
must 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]
重塑V
intoY
。
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 matrixM
into 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]
M
和N
must have the same number of elements.reshape
阅读M
column-wise to fill in the elements ofN
column-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]
重塑M
into 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 arrayM
into a 9-by-2 matrix.
M
has 18 elements. Because a 9-by-2 matrix also has 18 elements,M
can 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)]
重塑M
into 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)]
使用重塑分解阵列
Usereshape
instead of loops to break up arrays for further computation. Usereshape
分解矢量V
to 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]
Specify3
for 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)]
reshape
calculates that five columns are required for a matrix of three rows.prod
then multiples the elements of each column to return the result.