Fill an input ndarray with a specified value.
var fill = require( '@stdlib/ndarray/base/fill' );Fills an input ndarray with a specified value.
var array = require( '@stdlib/ndarray/array' );
var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] );
// returns <ndarray>[ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ]
var out = fill( x, 10.0 );
// returns <ndarray>[ [ [ 10.0, 10.0 ] ], [ [ 10.0, 10.0 ] ], [ [ 10.0, 10.0 ] ] ]
var bool = ( out === x );
// returns trueThe function accepts the following arguments:
- x: input ndarray.
- value: scalar value.
-
A provided ndarray should be an object with the following properties:
- dtype: data type.
- data: data buffer.
- shape: dimensions.
- strides: stride lengths.
- offset: index offset.
- order: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style).
-
If
valueis a number andxhas a complex data type, the function fills an input ndarray with a complex number whose real component equals the provided scalarvalueand whose imaginary component is zero. -
A
valuemust be able to safely cast to the input ndarray data type. Scalar values having floating-point data types (both real and complex) are allowed to downcast to a lower precision data type of the same kind (e.g., a scalar double-precision floating-point number can be used to fill a'float32'input ndarray). -
The function mutates the input ndarray.
var discreteUniform = require( '@stdlib/random/discrete-uniform' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var fill = require( '@stdlib/ndarray/base/fill' );
var x = discreteUniform( [ 5, 2 ], -100, 100, {
'dtype': 'generic'
});
console.log( ndarray2array( x ) );
fill( x, 10.0 );
console.log( ndarray2array( x ) );