Skip to content

Latest commit

 

History

History
171 lines (106 loc) · 4.54 KB

File metadata and controls

171 lines (106 loc) · 4.54 KB

last

Return a read-only view of the last element (or subarray) along one or more ndarray dimensions.

Usage

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

last( x[, options] )

Returns a read-only view of the last element (or subarray) along one or more ndarray dimensions.

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

var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] );
// returns <ndarray>[ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]

var v = last( x );
// returns <ndarray>[ 4.0 ]

The function accepts the following arguments:

  • x: input ndarray.
  • options: function options.

The function accepts the following options:

  • dims: list of dimensions over which to perform the operation. If a dimension index is provided as an integer less than zero, the dimension index is resolved relative to the last dimension, with the last dimension corresponding to the value -1. By default, the function performs the operation over all dimensions.

To resolve the last element (or subarray) along one or more specified dimensions, provide a dims option:

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

var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] );
// returns <ndarray>[ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]

// Last column:
var v = last( x, {
    'dims': [ -1 ]
});
// returns <ndarray>[ 2.0, 4.0 ]

// Last row:
v = last( x, {
    'dims': [ -2 ]
});
// returns <ndarray>[ 3.0, 4.0 ]

Notes

  • The function always returns a read-only view. To convert a returned view to a writable ndarray, copy the contents to a new ndarray (e.g., via @stdlib/ndarray/copy).
  • If provided an empty dims array, the function returns a read-only view of the input ndarray.

Examples

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

var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 );
console.log( ndarray2array( x ) );

// Last scalar element:
var v = last( x );
console.log( v.get() );

// Last columns along the innermost dimension:
v = last( x, {
    'dims': [ -1 ]
});
console.log( ndarray2array( v ) );

// Last matrix along the outermost dimension:
v = last( x, {
    'dims': [ 0 ]
});
console.log( ndarray2array( v ) );