函数 reshape #

将多维数组重塑以适应指定的维度

Syntax #

math.reshape(x, sizes)

Parameters #

Parameter Type Description
x Array | Matrix | * 要重塑的矩阵
sizes number[] 一维整数数组,指定每个维度的尺寸。允许一个 -1 作为通配符,它会自动计算该维度。

Returns #

Type Description
* | Array | Matrix 矩阵 x 重塑后的克隆

Throws #

Type Description
TypeError 如果 sizes 不仅包含整数
DimensionError 如果新维度尺寸的乘积不等于旧维度尺寸的乘积

Examples #

 math.reshape([1, 2, 3, 4, 5, 6], [2, 3])
 // returns Array  [[1, 2, 3], [4, 5, 6]]

 math.reshape([[1, 2], [3, 4]], [1, 4])
 // returns Array  [[1, 2, 3, 4]]

 math.reshape([[1, 2], [3, 4]], [4])
 // returns Array [1, 2, 3, 4]

 const x = math.matrix([1, 2, 3, 4, 5, 6, 7, 8])
 math.reshape(x, [2, 2, 2])
 // returns Matrix [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]

math.reshape([1, 2, 3, 4], [-1, 2])
// returns Matrix [[1, 2], [3, 4]]

另请参阅 #

size, squeeze, resize

Fork me on GitHub