Public
Edited
Jul 10, 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
m.index([1])
Insert cell
m.index([])
Insert cell
function isFlatArray(X){
X.forEach(x=>{
if(Array.isArray(x)) {
return false
}
})
return true
}
Insert cell
isFlatArray([1,2])
Insert cell
isFlatArray([[1],[2,2]])
Insert cell
m.matrix([true, false])

Insert cell
emptyIndex = m.index([])
Insert cell
notEmptyIndex = m.index([1])
Insert cell
m.index([1,2])
Insert cell
notEmptyIndex._dimensions.length
Insert cell
isEmptyIndex(m.index([1,2])) // should be false
Insert cell
isEmptyIndex(m.index([])) // shuld be true
Insert cell
isEmptyIndex(m.index([],[1])) // should be true
Insert cell
isEmptyIndex(m.index([],[1])) // should be true
Insert cell
isEmptyIndex(m.index(m.range(0,2),1))
Insert cell
function isEmptyIndex(index) {
for (let i = 0; i < index._dimensions.length; ++i) {
if (index._dimensions[i]._size[0] === 0){
return true
}
}
return false
}
Insert cell
m.index([],[1])._dimensions[0]._size[0] === 0
Insert cell
m.index(m.range(0,3),1)
Insert cell
isEmptyIndex(m.index(0)) // should be false
Insert cell
m.index([0,1])
Insert cell
m.help("index)
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