Function reshape #

Reshape a multi dimensional array to fit the specified dimensions

Syntax #

math.reshape(x, sizes)

Parameters #

Parameter Type Description
x Array | Matrix | * Matrix to be reshaped
sizes number[] One dimensional array with integral sizes for each dimension. One -1 is allowed as wildcard, which calculates this dimension automatically.

Returns #

Type Description
* | Array | Matrix A reshaped clone of matrix x

Throws #

Type Description
TypeError If sizes does not contain solely integers
DimensionError If the product of the new dimension sizes does not equal that of the old ones

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]]

See also #

size, squeeze, resize

Fork me on GitHub