Return a new array with all elements within a specified range replaced with a provided value.
var toFilled = require( '@stdlib/array/base/to-filled' );Returns a new array with all elements within a specified range replaced with a provided value.
var x = [ 1, 2, 3, 4 ];
var out = toFilled( x, 5, 1, 3 );
// returns [ 1, 5, 5, 4 ]
out = toFilled( x, 6, -3, -1 );
// returns [ 1, 6, 6, 4 ]The function accepts the following arguments:
- x: an input array.
- value: fill value.
- start: starting index (inclusive).
- end: ending index (exclusive).
Copies elements from one array to another array and replaces all elements within a specified range with a provided value.
var x = [ 1, 2, 3, 4 ];
var out = [ 0, 0, 0, 0 ];
var arr = toFilled.assign( x, 5, 1, 3, out, 1, 0 );
// returns [ 1, 5, 5, 4 ]
var bool = ( arr === out );
// returns trueThe function accepts the following arguments:
- x: an input array.
- value: fill value.
- start: starting index (inclusive).
- end: ending index (exclusive).
- out: output array.
- stride: output array stride.
- offset: output array offset.
- Negative indices are resolved relative to the last array element, with the last element corresponding to
-1.
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var logEachMap = require( '@stdlib/console/log-each-map' );
var naryFunction = require( '@stdlib/utils/nary-function' );
var papply = require( '@stdlib/utils/papply' );
var toFilled = require( '@stdlib/array/base/to-filled' );
// Define an array:
var opts = {
'dtype': 'generic'
};
var x = discreteUniform( 10, -100, 100, opts );
// Define an array containing random fill values:
var values = discreteUniform( 100, -10000, 10000, opts );
// Define parallel arrays containing random fill ranges:
var starts = discreteUniform( values.length, 0, x.length-1, opts );
var ends = discreteUniform( values.length, 1, x.length, opts );
// Randomly fill ranges of the input array:
logEachMap( 'fill with %d in [%d, %d) => x = [%s]', values, starts, ends, naryFunction( papply( toFilled, x ), 3 ) );