ubique
Version:
A mathematical and quantitative library for Javascript and Node.js
35 lines • 809 B
JavaScript
/**
* Indexing
*/
module.exports = function($u) {
/**
* @method find
* @summary Find indices of nonzero elements
* @description Find indices of nonzero elements
*
* @param {array|matrix} x values
* @return {array|matrix}
*
* @example
* ubique.find([0.3,-0.4,0.5,0.9].map(function(a){return a > 0}));
* // [ 0, 2, 3 ]
*
* ubique.find([[true,true],[false,true]]);
* // [ 0, 1, 3 ]
*/
$u.find = function(x) {
if (arguments.length === 0) {
throw new Error('not enough input arguments');
}
if ($u.isnumber(x)) {
throw Error('input must be an array or matrix');
}
if ($u.ismatrix(x)) {
x = $u.flatten(x); // flatten by rows
}
var dummy = $u.colon(0,x.length);
return dummy.filter(function(el) {
return x[el] === true;
})
}
}