normalizeIndex
Normalize an index to the interval
[0,max]
.
Usage
var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' );
normalizeIndex( idx, max )
Normalizes an index to the interval [0,max]
.
var idx = normalizeIndex( 2, 10 );
// returns 2
idx = normalizeIndex( -5, 10 );
// returns 6
If provided an out-of-bounds index, the function returns -1
.
var idx = normalizeIndex( 15, 10 );
// returns -1
idx = normalizeIndex( -15, 10 );
// returns -1
Examples
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' );
var idx;
var out;
var i;
for ( i = 0; i < 100; i++ ) {
idx = discreteUniform( -20, 20 );
out = normalizeIndex( idx, 15 );
console.log( '%d => [%d,%d] => %d', idx, 0, 15, out );
}