function find
find(x: array<boolean | number> | matrix<boolean | number>): array

Finds the indices of nonzero (true) elements in an array or matrix.

Given an input array or matrix, returns the indices of elements that are true. For a matrix, the indices are flattened row-wise.

Examples

Find indices of true elements in a 1D array

import { assertEquals } from "jsr:@std/assert";

find([0.3, -0.4, 0.5, 0.9].map(a => a > 0)); // [0, 2, 3]

Find indices of true elements in a 2D matrix

import { assertEquals } from "jsr:@std/assert";

find([[true, true], [false, true]]); // [0, 1, 3]

Empty input

import { assertEquals } from "jsr:@std/assert";

find([]); // []

No true elements

import { assertEquals } from "jsr:@std/assert";

find([false, false, false]); // []

All true elements

import { assertEquals } from "jsr:@std/assert";

find([true, true, true]); // [0, 1, 2]

Parameters

x: array<boolean | number> | matrix<boolean | number>

Input array or matrix.

Return Type

An array of indices where the values are true.

Throws

If no arguments are provided or if the input is not an array or matrix.