Apply a condition to elements in two input ndarrays and assign results to elements in an output ndarray.
var where = require( '@stdlib/ndarray/base/where' );Applies a condition to elements in two input ndarrays and assigns results to elements in an output ndarray.
var Float64Array = require( '@stdlib/array/float64' );
var Uint8Array = require( '@stdlib/array/uint8' );
// Create data buffers:
var cbuf = new Uint8Array( [ 1, 0, 0, 1, 0, 1 ] );
var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
var ybuf = new Float64Array( [ -1.0, -2.0, -3.0, -4.0, -5.0, -6.0 ] );
var obuf = new Float64Array( 6 );
// Define the shape of the input and output arrays:
var shape = [ 3, 1, 2 ];
// Define the array strides:
var sc = [ 2, 2, 1 ];
var sx = [ 2, 2, 1 ];
var sy = [ 2, 2, 1 ];
var so = [ 2, 2, 1 ];
// Define the index offsets:
var oc = 0;
var ox = 0;
var oy = 0;
var oo = 0;
// Create the input and output ndarrays:
var condition = {
'dtype': 'uint8',
'data': cbuf,
'shape': shape,
'strides': sc,
'offset': oc,
'order': 'row-major'
};
var x = {
'dtype': 'float64',
'data': xbuf,
'shape': shape,
'strides': sx,
'offset': ox,
'order': 'row-major'
};
var y = {
'dtype': 'float64',
'data': ybuf,
'shape': shape,
'strides': sy,
'offset': oy,
'order': 'row-major'
};
var out = {
'dtype': 'float64',
'data': obuf,
'shape': shape,
'strides': so,
'offset': oo,
'order': 'row-major'
};
// Apply the condition:
where( [ condition, x, y, out ] );
console.log( out.data );
// => <Float64Array>[ 1.0, -2.0, -3.0, 4.0, -5.0, 6.0 ]The function accepts the following arguments:
- arrays: array-like object containing three input ndarrays and one output ndarray.
Each 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).
conditionndarray must be abooleanoruint8ndarray.condition,x,y, andoutndarrays must have the same shape.- For very high-dimensional ndarrays which are non-contiguous, one should consider copying the underlying data to contiguous memory before conditionally assigning elements in order to achieve better performance.
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
var bernoulli = require( '@stdlib/random/base/bernoulli' ).factory;
var filledarray = require( '@stdlib/array/filled' );
var filledarrayBy = require( '@stdlib/array/filled-by' );
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
var where = require( '@stdlib/ndarray/base/where' );
var N = 10;
var shape = [ 5, 2 ];
var condition = {
'dtype': 'uint8',
'data': filledarrayBy( N, 'uint8', bernoulli( 0.5 ) ),
'shape': shape,
'strides': [ 2, 1 ],
'offset': 0,
'order': 'row-major'
};
console.log( ndarray2array( condition.data, condition.shape, condition.strides, condition.offset, condition.order ) ); // eslint-disable-line max-len
var x = {
'dtype': 'generic',
'data': filledarrayBy( N, 'generic', discreteUniform( 0, 100 ) ),
'shape': shape,
'strides': [ 2, 1 ],
'offset': 0,
'order': 'row-major'
};
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
var y = {
'dtype': 'generic',
'data': filledarrayBy( N, 'generic', discreteUniform( -100, 0 ) ),
'shape': shape,
'strides': [ 2, 1 ],
'offset': 0,
'order': 'row-major'
};
console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) );
var out = {
'dtype': 'generic',
'data': filledarray( 0, N, 'generic' ),
'shape': shape.slice(),
'strides': shape2strides( shape, 'column-major' ),
'offset': 0,
'order': 'column-major'
};
where( [ condition, x, y, out ] );
console.log( ndarray2array( out.data, out.shape, out.strides, out.offset, out.order ) ); // eslint-disable-line max-len