Public
Edited
Jul 8, 2023
Insert cell
Insert cell
m = require("mathjs@11.8")
Insert cell
function isBooleanIndex(x) {
return isFlat(x) && m.getMatrixDataType(x) === "boolean";
}
Insert cell
function isFlat(x) {
if (m.isArray(x)) {
return m.size(x).length === 1;
} else if (m.isMatrix(x)) {
return x._size.length === 1;
} else {
return false;
}
}
Insert cell
function convertBooleanToIndex(X) {
const ind = [];
X.forEach((x, i) => {
if (x) ind.push(i);
});
return ind;
}
Insert cell
function convertBooleanToIndexMap (booleans) {
// convert an array of booleans to index
return booleans.map((_, i) => i).filter((_, i) => booleans[i])
}
Insert cell
convertBooleanToIndex([false])
Insert cell
convertBooleanToIndex([true, false, true, true, false, true])
Insert cell
convertBooleanToIndexMap([true, false, true, true, false, true])
Insert cell
isBooleanIndex([true, false, false])
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more