function ind2sub
ind2sub(
size: array,
index: number | array,
): array | matrix

Converts linear index to row and column subscripts.

Converts a linear index or array of linear indices into the equivalent row and column subscripts for a given matrix size. This allows converting a 1D index into 2D row/column positions.

Examples

Convert linear index 5 to subscripts in a 2x3 matrix

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

assertEquals(ind2sub([2, 3], 5), [1, 2]);

Convert multiple linear indices to subscripts

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

assertEquals(ind2sub([2, 3], [0, 1, 2]), [[0, 0], [1, 0], [0, 1]]);

Convert linear index to subscripts in a vector (3x1 matrix)

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

assertEquals(ind2sub([3, 1], 2), [2, 0]);

Parameters

size: array

Size of the matrix as [rows, columns].

index: number | array

Linear index or array of indices [0...N-1].

Return Type

The corresponding row and column subscripts.

Throws

If no arguments are provided or if inputs are invalid.