Skip to content

Latest commit

 

History

History
163 lines (102 loc) · 4.14 KB

File metadata and controls

163 lines (102 loc) · 4.14 KB

toFilled

Return a new array with all elements within a specified range replaced with a provided value.

Usage

var toFilled = require( '@stdlib/array/base/to-filled' );

toFilled( x, value, start, end )

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).

toFilled.assign( x, value, start, end, out, stride, offset )

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 true

The 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.

Notes

  • Negative indices are resolved relative to the last array element, with the last element corresponding to -1.

Examples

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 ) );