Skip to content

Latest commit

 

History

History
173 lines (108 loc) · 4.52 KB

File metadata and controls

173 lines (108 loc) · 4.52 KB

pop

Return an array containing a read-only truncated view of an input ndarray and a read-only view of the last element(s) along a specified dimension.

Usage

var pop = require( '@stdlib/ndarray/pop' );

pop( x[, options] )

Returns an array containing a read-only truncated view of an input ndarray and a read-only view of the last element(s) along a specified dimension.

var array = require( '@stdlib/ndarray/array' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );

var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ], {
    'shape': [ 3, 2 ]
});
// returns <ndarray>

var arr = ndarray2array( x );
// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]

var y = pop( x );
// returns [ <ndarray>, <ndarray> ]

arr = ndarray2array( y[ 0 ] );
// returns [ [ 1.0 ], [ 3.0 ], [ 5.0 ] ]

arr = ndarray2array( y[ 1 ] );
// returns [ [ 2.0 ], [ 4.0 ], [ 6.0 ] ]

The function accepts the following arguments:

  • x: input ndarray. Must have one or more dimensions.
  • options: function options. (optional)

The function supports the following options:

  • dim: dimension along which to perform the operation. If provided an integer less than zero, the dimension index is resolved relative to the last dimension, with the last dimension corresponding to the value -1. Default: -1.

By default, the function performs the operation along the last dimension of the input ndarray. To perform the operation along a specific dimension, specify the dim option.

var array = require( '@stdlib/ndarray/array' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );

var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ], {
    'shape': [ 3, 2 ]
});
// returns <ndarray>

var arr = ndarray2array( x );
// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]

var opts = {
    'dim': 0
};

var y = pop( x, opts );
// returns [ <ndarray>, <ndarray> ]

arr = ndarray2array( y[ 0 ] );
// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]

arr = ndarray2array( y[ 1 ] );
// returns [ [ 5.0, 6.0 ] ]

Examples

var array = require( '@stdlib/ndarray/array' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var zeroTo = require( '@stdlib/array/zero-to' );
var pop = require( '@stdlib/ndarray/pop' );

// Create an ndarray:
var x = array( zeroTo( 27 ), {
    'shape': [ 3, 3, 3 ]
});
console.log( ndarray2array( x ) );

// Remove the last column from each matrix:
var y = pop( x );

console.log( ndarray2array( y[ 0 ] ) );
console.log( ndarray2array( y[ 1 ] ) );