Documentation

reshape

重塑阵列

Syntax

b =重塑(a,sz)
b = reshape(a,sz1,...,szn)

Description

example

B= reshape(A,sz)重塑Ausing the size vector,sz, to define尺寸(b)。For example,reshape(A,[2,3])重塑A进入2-by-3 matrix.szmust contain at least 2 elements, andprod(sz)must be the same asnumel(a)

example

B= reshape(A,sz1,...,szN)重塑A进入sz1-by-。。。-by-szNarray wheresz1,...,szNindicates the size of each dimension. You can specify a single dimension size of[]to have the dimension size automatically calculated, such that the number of elements inBmatches the number of elements inA。For example, ifAis a 10-by-10 matrix, thenreshape(A,2,2,[])重塑the 100 elements ofA进入2-by-2-by-25 array.

Examples

collapse all

将1 x 10向量重塑为5 x-2矩阵。

A = 1:10; B = reshape(A,[5,2])
b =1 6 2 7 3 8 4 9 5 10

Reshape a 6-by-6 magic square matrix into a matrix that has only 3 columns. Specify[]对于第一个维度大小reshape自动计算适当的行数。

A = magic(6); B = reshape(A,[],3)
b =35 6 19 3 7 23 31 2 27 8 33 10 30 34 14 4 29 18 1 26 24 32 21 25 9 22 20 28 17 15

结果是一个12 x-3矩阵,该矩阵保持与原始6 x-6矩阵相同数量的元素(36)。元素中的元素B还从A

Reshape a 3-by-2-by-3 array of zeros into a 9-by-2 matrix.

a =零(3,2,3);b =重塑(A,9,2)
b =0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Input Arguments

collapse all

Input array, specified as a vector, matrix, or multidimensional array.

Data Types:single|double|int8|INT16|int32|int64|uint8|uint16|uint32|uint64|logical|char|string|cell|约会时间|duration|calendarDuration
Complex Number Support:Yes

输出大小,指定为整数的行矢量。每个元素szindicates the size of the corresponding dimension inB。您必须指定sz这样的元素数量AB是相同的。那是,prod(sz)must be the same asnumel(a)

Beyond the second dimension, the output,B, does not reflect trailing dimensions with a size of1。For example,reshape(A,[3,2,1,1])produces a 3-by-2 matrix.

Example:reshape(A,[3,2])

Example:重塑(A,[6,4,10])

Example:重塑(A,[5,5,5,5])

每个维度的大小,指定为两个或多个整数,最多有一个[](optional). You must specify at least 2 dimension sizes, and at most one dimension size can be specified as[], which automatically calculates the size of that dimension to ensure thatnumel(B)matchesnumel(a)。When you use[]to automatically calculate a dimension size, the dimensions that youdoexplicitly specify must divide evenly into the number of elements in the input matrix,numel(a)

Beyond the second dimension, the output,B, does not reflect trailing dimensions with a size of1。For example,reshape(A,3,2,1,1)produces a 3-by-2 matrix.

Example:reshape(A,3,2)

Example:reshape(A,6,[],10)

Example:重塑(A,2,5,3,[])

Example:重塑(A,5,5,5,5)

Output Arguments

collapse all

Reshaped array, returned as a vector, matrix, multidimensional array, or cell array. The data type and number of elements inBare the same as the data type and number of elements inA。元素中的元素B保留他们的圆柱订购A

Data Types:single|double|int8|INT16|int32|int64|uint8|uint16|uint32|uint64|logical|char|string|cell|约会时间|duration|calendarDuration

Extended Capabilities

在R2006a之前引入

这个话题有帮助吗?