Reshape a nested array into another nested array having a desired shape.
var reshape = require( '@stdlib/array/base/reshape' );
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 ] ]
- The function assumes that
fromShape
andtoShape
describe arrays having the same number of elements.
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 ] ]