takeIndexed2
Take elements from two indexed arrays in a single pass.
Usage
var takeIndexed2 = require( '@stdlib/array/base/take-indexed2' );
takeIndexed2( x, y, indices )
Takes elements from two indexed arrays in a single pass.
var x = [ 1, 2, 3, 4 ];
var y = [ 5, 6, 7, 8 ];
var out = takeIndexed2( x, y, [ 1, 3 ] );
// returns [ [ 2, 4 ], [ 6, 8 ] ]
If indices
is an empty array, the function returns empty arrays.
var x = [ 1, 2, 3, 4 ];
var y = [ 5, 6, 7, 8 ];
var out = takeIndexed2( x, y, [] );
// returns [ [], [] ]
Notes
- The function does not perform bounds checking. If an index is less than zero or greater than the maximum index of
x
ory
, the value of the corresponding element in the respective output array is undefined. - An indexed array-like object is a data structure in which one retrieves elements via integer indices using bracket
[]
notation (e.g.,Float64Array
,Int32Array
,Array
, etc). This is in contrast to an accessor array-like object in which one retrieves elements usingget
andset
methods (e.g.,Complex64Array
andComplex128Array
).
Examples
var filledBy = require( '@stdlib/array/base/filled-by' );
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
var linspace = require( '@stdlib/array/base/linspace' );
var takeIndexed2 = require( '@stdlib/array/base/take-indexed2' );
// Generate linearly spaced arrays:
var x = linspace( 0, 100, 11 );
console.log( x );
var y = linspace( 100, 200, 11 );
console.log( y );
// Generate an array of random indices:
var N = discreteUniform( 5, 15 );
var indices = filledBy( N, discreteUniform.factory( 0, x.length-1 ) );
console.log( indices );
// Take a random sample of elements from `x` and `y`:
var out = takeIndexed2( x, y, indices );
console.log( out );