Skip to content

Latest commit

 

History

History
122 lines (75 loc) · 2.82 KB

File metadata and controls

122 lines (75 loc) · 2.82 KB

reshape

Reshape a nested array into another nested array having a desired shape.

Usage

var reshape = require( '@stdlib/array/base/reshape' );

reshape( x, fromShape, toShape, colexicographic )

Reshapes a nested array into another nested array having a desired shape.

var x = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ];

var out = reshape( x, [ 2, 3 ], [ 3, 2 ], false );
// returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ]
  • x: input array.
  • fromShape: input array shape.
  • toShape: output array shape.
  • colexicographic: boolean indicating whether to reshape the array in colexicographic order.

To reshape in colexicographic order, set the colexicographic argument to true.

var x = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ];

var out = reshape( x, [ 2, 3 ], [ 3, 2 ], true );
// [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]

Notes

  • The function assumes that fromShape and toShape describe arrays having the same number of elements.

Examples

var reshape = require( '@stdlib/array/base/reshape' );

var x = [
    [ 1, 2, 3, 4 ],
    [ 5, 6, 7, 8 ],
    [ 9, 10, 11, 12 ]
];

var out = reshape( x, [ 3, 4 ], [ 4, 3 ], false );
// returns [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ], [ 10, 11, 12 ] ]

out = reshape( x, [ 3, 4 ], [ 6, 2 ], false );
// returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ], [ 9, 10 ], [ 11, 12 ] ]

out = reshape( x, [ 3, 4 ], [ 1, 12 ], false );
// returns [ [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ]

out = reshape( x, [ 3, 4 ], [ 12, 1 ], false );
// returns [ [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ], [ 6 ], [ 7 ], [ 8 ], [ 9 ], [ 10 ], [ 11 ], [ 12 ] ]