@stdlib/array-base-reshape
v0.1.1
Published
Reshape a nested array into another nested array having a desired shape.
Readme
reshape
Reshape a nested array into another nested array having a desired shape.
Installation
npm install @stdlib/array-base-reshapeUsage
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
fromShapeandtoShapedescribe 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 ] ]Notice
This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
Community
License
See LICENSE.
Copyright
Copyright © 2016-2026. The Stdlib Authors.
