Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

ndarrayWith

Return a new ndarray with the element at a specified index replaced by a provided value.

Usage

var ndarrayWith = require( '@stdlib/ndarray/with' );

ndarrayWith( x, indices, value )

Returns a new ndarray with the element at a specified index replaced by a provided value.

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

var x = zeros( [ 2, 2 ], {
    'dtype': 'float64'
});
// returns <ndarray>

var out = ndarrayWith( x, [ 0, 0 ], 1.0 );
// returns <ndarray>

var v = out.get( 0, 0 );
// returns 1.0

The function accepts the following arguments:

  • x: input ndarray.
  • indices: indices of the element to replace.
  • value: replacement value.

Notes

  • This function does not validate that a provided value is compatible with the data type of the input ndarray. For example, the function does not guard against precision loss when value is a real-valued number and the input ndarray has an integer data type. This function should be considered a copy-on-write analog to using an ndarray's set method. Whether a value is silently coerced to the data type of the input ndarray or triggers an exception when incompatible is implementation-dependent. Accordingly, any assertion logic ensuring data type compatibility should be performed before calling this function.

Examples

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

var x = zeros( [ 1, 3, 3 ], {
    'dtype': 'float64'
});

var out = ndarrayWith( x, [ 0, 1, 1 ], 10.0 );
console.log( ndarray2array( out ) );