reshape

Syntax

reshape(obj, [dim])

Parameters

obj is a vector/matrix.

dim (optional) is a pair of integers indicating (row dimension):(column dimension) of the result.

Details

Change the dimensions of a matrix and return a new matrix. If dim is not specified, reshape obj to a vector.

Both DolphinDB’s reshape(obj, dim) and NumPy’s numpy.reshape(a, shape) are used to reshape data, but their behavior differs in several aspects:

  • DolphinDB only supports vectors and matrices. The target dimensions are specified using a pair, and data is filled in column-major order by default, making its behavior closer to NumPy with order="F".
  • NumPy supports arbitrary-dimensional ndarray objects and fills data in row-major order by default (order="C"). It also supports automatic dimension inference, as well as parameters such as order and copy.
  • In addition, when dim is omitted in DolphinDB, a matrix is flattened into a vector. In NumPy, the target shape must always be specified explicitly, and flattening is typically achieved with reshape(-1).

Examples

x=1..6;
x=x.reshape(3:2);
x
#0 #1
1 4
2 5
3 6
x=x.reshape(2:3);
x
#0 #1 #2
1 3 5
1 4 6
x=x.reshape(6:1)
x
#0
1
2
3
4
5
6
x.reshape()
// output: [1,2,3,4,5,6]
// reshape x to a vector.