function flatten
flatten(x: number): number

Flatten a matrix into an array.

Flattens a matrix into a 1D array. The default concatenation is row-wise (dim = 0). If dim = 1, the concatenation is column-wise.

Examples

Flatten a 2x2 matrix (row-wise by default)

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

assertEquals(flatten([[5, 6], [7, 8]]), [5, 6, 7, 8]);

Flatten a 3x3 matrix by rows

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

assertEquals(flatten([[1, 1, -1], [1, -2, 3], [2, 3, 1]]), [1, 1, -1, 1, -2, 3, 2, 3, 1]);

Flatten a 3x3 matrix by columns (dim = 1)

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

assertEquals(flatten([[1, 1, -1], [1, -2, 3], [2, 3, 1]], 1), [1, 1, 2, 1, -2, 3, -1, 3, 1]);

Parameters

x: number

The matrix to flatten.

Return Type

number

The flattened 1D array.

Throws

If no input is provided or if the input is not a matrix.

flatten(x: array): array

Flatten a matrix into an array.

Flattens a matrix into a 1D array. The default concatenation is row-wise (dim = 0). If dim = 1, the concatenation is column-wise.

Examples

Flatten a 2x2 matrix (row-wise by default)

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

assertEquals(flatten([[5, 6], [7, 8]]), [5, 6, 7, 8]);

Flatten a 3x3 matrix by rows

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

assertEquals(flatten([[1, 1, -1], [1, -2, 3], [2, 3, 1]]), [1, 1, -1, 1, -2, 3, 2, 3, 1]);

Flatten a 3x3 matrix by columns (dim = 1)

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

assertEquals(flatten([[1, 1, -1], [1, -2, 3], [2, 3, 1]], 1), [1, 1, 2, 1, -2, 3, -1, 3, 1]);

Parameters

The matrix to flatten.

Return Type

The flattened 1D array.

Throws

If no input is provided or if the input is not a matrix.

flatten(
x: matrix,
dim: 0 | 1,
): array

Flatten a matrix into an array.

Flattens a matrix into a 1D array. The default concatenation is row-wise (dim = 0). If dim = 1, the concatenation is column-wise.

Examples

Flatten a 2x2 matrix (row-wise by default)

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

assertEquals(flatten([[5, 6], [7, 8]]), [5, 6, 7, 8]);

Flatten a 3x3 matrix by rows

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

assertEquals(flatten([[1, 1, -1], [1, -2, 3], [2, 3, 1]]), [1, 1, -1, 1, -2, 3, 2, 3, 1]);

Flatten a 3x3 matrix by columns (dim = 1)

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

assertEquals(flatten([[1, 1, -1], [1, -2, 3], [2, 3, 1]], 1), [1, 1, 2, 1, -2, 3, -1, 3, 1]);

Parameters

The matrix to flatten.

dim: 0 | 1

Return Type

The flattened 1D array.

Throws

If no input is provided or if the input is not a matrix.

flatten(
dim?: 0 | 1,
): array

Flatten a matrix into an array.

Flattens a matrix into a 1D array. The default concatenation is row-wise (dim = 0). If dim = 1, the concatenation is column-wise.

Examples

Flatten a 2x2 matrix (row-wise by default)

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

assertEquals(flatten([[5, 6], [7, 8]]), [5, 6, 7, 8]);

Flatten a 3x3 matrix by rows

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

assertEquals(flatten([[1, 1, -1], [1, -2, 3], [2, 3, 1]]), [1, 1, -1, 1, -2, 3, 2, 3, 1]);

Flatten a 3x3 matrix by columns (dim = 1)

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

assertEquals(flatten([[1, 1, -1], [1, -2, 3], [2, 3, 1]], 1), [1, 1, 2, 1, -2, 3, -1, 3, 1]);

Parameters

The matrix to flatten.

optional
dim: 0 | 1

Return Type

The flattened 1D array.

Throws

If no input is provided or if the input is not a matrix.