Skip to content

Latest commit

 

History

History
152 lines (96 loc) · 3.44 KB

File metadata and controls

152 lines (96 loc) · 3.44 KB

nanmidrangeBy

Calculate the midrange of an array via a callback function, ignoring NaN values.

The midrange is defined as the arithmetic mean of the maximum and minimum values.

Usage

var nanmidrangeBy = require( '@stdlib/stats/array/nanmidrange-by' );

nanmidrangeBy( x, clbk[, thisArg] )

Computes the midrange of an array via a callback function, ignoring NaN values.

function accessor( v ) {
    return v * 2.0;
}

var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, NaN, 0.0, -1.0, -3.0 ];

var v = nanmidrangeBy( x, accessor );
// returns -1.0

The function has the following parameters:

  • x: input array.
  • clbk: callback function.
  • thisArg: execution context (optional).

The invoked callback is provided three arguments:

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

To set the callback execution context, provide a thisArg.

function accessor( v ) {
    this.count += 1;
    return v * 2.0;
}

var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, NaN, 0.0, -1.0, -3.0 ];

var context = {
    'count': 0
};

var v = nanmidrangeBy( x, accessor, context );
// returns -1.0

var cnt = context.count;
// returns 9

Notes

  • If provided an empty array, 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.
  • The function supports array-like objects having getter and setter accessors for array element access (e.g., @stdlib/array/base/accessor).

Examples

var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var nanmidrangeBy = require( '@stdlib/stats/array/nanmidrange-by' );

function accessor( v ) {
    return v * 2.0;
}

var x = discreteUniform( 10, -50, 50, {
    'dtype': 'float64'
});
x[ 2 ] = NaN;
console.log( x );

var v = nanmidrangeBy( x, accessor );
console.log( v );