Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

shift

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

Usage

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

shift( x[, options] )

Returns an array containing a read-only truncated view of an input ndarray and a read-only view of the first 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 = shift( x );
// returns [ <ndarray>, <ndarray> ]

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

arr = ndarray2array( y[ 1 ] );
// returns [ [ 1.0 ], [ 3.0 ], [ 5.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 operation along the last dimension of the input ndarray. To perform operation along a specific dimension, provide 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 = shift( x, opts );
// returns [ <ndarray>, <ndarray> ]

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

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

Examples

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

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

// Remove the first column from each matrix:
var y = shift( x );

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