Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

first

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

Usage

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

first( x[, options] )

Returns a read-only view of the first 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 = first( x );
// returns <ndarray>[ 1.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 first 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 ] ]

// First column:
var v = first( x, {
    'dims': [ -1 ]
});
// returns <ndarray>[ 1.0, 3.0 ]

// First row:
v = first( x, {
    'dims': [ -2 ]
});
// returns <ndarray>[ 1.0, 2.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 first = require( '@stdlib/ndarray/first' );

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

// First scalar element:
var v = first( x );
console.log( v.get() );

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

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