Wrap Index

Wrap an index on the interval [0,max].

Usage

var wrapIndex = require( '@stdlib/ndarray/base/wrap-index' );

wrapIndex( idx, max )

Wrap an index on the interval [0,max].

var idx = wrapIndex( 2, 10 );
// returns 2

idx = wrapIndex( -5, 10 );
// returns 6

idx = wrapIndex( 15, 10 );
// returns 4

Examples

var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
var wrapIndex = require( '@stdlib/ndarray/base/wrap-index' );

var idx;
var out;
var i;

for ( i = 0; i < 100; i++ ) {
    idx = discreteUniform( -20, 20 );
    out = wrapIndex( idx, 9 );
    console.log( '%d => [%d,%d] => %d', idx, 0, 9, out );
}
Did you find this page helpful?