Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

nanrangeBy

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

The range is defined as the difference between the maximum and minimum values.

Usage

var nanrangeBy = require( '@stdlib/stats/array/nanrange-by' );

nanrangeBy( x, clbk[, thisArg] )

Computes the range 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, NaN ];

var v = nanrangeBy( x, accessor );
// returns 18.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, NaN ];

var context = {
    'count': 0
};

var v = nanrangeBy( x, accessor, context );
// returns 18.0

var cnt = context.count;
// returns 10

Notes

  • If provided an empty array, the function returns NaN.
  • A provided callback function should return a numeric value.
  • If a provided callback function returns NaN, the value is ignored.
  • 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 uniform = require( '@stdlib/random/base/uniform' );
var bernoulli = require( '@stdlib/random/base/bernoulli' );
var filledarrayBy = require( '@stdlib/array/filled-by' );
var nanrangeBy = require( '@stdlib/stats/array/nanrange-by' );

function rand() {
    if ( bernoulli( 0.8 ) < 1 ) {
        return NaN;
    }
    return uniform( -50.0, 50.0 );
}

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

var x = filledarrayBy( 10, 'float64', rand );
console.log( x );

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