Skip to content

Latest commit

 

History

History
152 lines (98 loc) · 3.46 KB

File metadata and controls

152 lines (98 loc) · 3.46 KB

nanmaxBy

Compute the maximum value of a one-dimensional ndarray via a callback function, ignoring NaN values.

Usage

var nanmaxBy = require( '@stdlib/stats/base/ndarray/nanmax-by' );

nanmaxBy( arrays, clbk[, thisArg ] )

Computes the maximum value of a one-dimensional ndarray via a callback function, ignoring NaN values.

var ndarray = require( '@stdlib/ndarray/base/ctor' );

function clbk( value ) {
    return value * 2.0;
}

var xbuf = [ 1.0, NaN, 4.0, 2.0 ];
var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );

var v = nanmaxBy( [ x ], clbk );
// returns 8.0

The function has the following parameters:

  • arrays: array-like object containing a one-dimensional input ndarray.
  • clbk: callback function.
  • thisArg: callback execution context (optional).

The invoked callback is provided three arguments:

  • value: current array element.
  • idx: current array element index.
  • array: input ndarray.

To set the callback execution context, provide a thisArg.

var ndarray = require( '@stdlib/ndarray/base/ctor' );

function clbk( value ) {
    this.count += 1;
    return value * 2.0;
}

var xbuf = [ 1.0, 3.0, NaN, 2.0 ];
var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
var ctx = {
    'count': 0
};

var v = nanmaxBy( [ x ], clbk, ctx );
// returns 6.0

var count = ctx.count;
// returns 4

Notes

  • If provided an empty one-dimensional ndarray, the function returns NaN.
  • A provided callback function should return a numeric value.
  • If a provided callback function does not return any value (or equivalently, explicitly returns undefined), the value is ignored.

Examples

var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var ndarray = require( '@stdlib/ndarray/base/ctor' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var nanmaxBy = require( '@stdlib/stats/base/ndarray/nanmax-by' );

function clbk( value ) {
    return value * 2.0;
}

var xbuf = discreteUniform( 10, -50, 50, {
    'dtype': 'generic'
});
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
console.log( ndarray2array( x ) );

var v = nanmaxBy( [ x ], clbk );
console.log( v );