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

Converts 2D subscripts to linear indices.

Converts 2D coordinates [X, Y] into linear indices based on the given matrix size.

Examples

Example 1

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

var a = [[5,6,5],[7,8,-1]];

Convert single 2D coordinate to linear index

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

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

Convert multiple 2D coordinates to linear indices

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

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

Convert index for a row vector

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

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

Parameters

size: array

The size of the matrix.

index: array | matrix

X, Y coordinates in the range [0...N-1].

Return Type

number | array

The computed linear index or an array of indices.

Throws

If input arguments are missing or invalid.