function end
end(
x:
number
| array
| matrix
,
dim?: number,
): number | array

Returns the last index in an array or matrix.

Returns the last index of an array or matrix. For matrices, you can specify the dimension: -1 for both rows and columns, 0 for rows, and 1 for columns.

Examples

Last index of a vector

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

assertEquals(end([5, 6, 3]), 2);

Last indices of a matrix

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

assertEquals(end([[4, 5, 0], [-1, 2, -3]]), [1, 2]);

Last row index of a matrix

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

assertEquals(end([[4, 5, 0], [-1, 2, -3]], 0), 1);

Last column index of a matrix

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

assertEquals(end([[4, 5, 0], [-1, 2, -3]], 1), 2);

Last index of a number (returns the number itself)

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

assertEquals(end(5), 5);

Parameters

x:
number
| array
| matrix

Input array or matrix.

optional
dim: number

For matrix: -1 (both), 0 (rows), 1 (columns). Defaults to -1.

Return Type

number | array

Last index or indices.

Throws

If no arguments are provided or if the dimension is invalid.