function repmat
repmat<T>(
x: matrix<T>,
m: number,
n?: number,
): matrix<T>

Replicate and tile an array or matrix.

Creates a new matrix by repeating the input value, array, or matrix in a tiled fashion. If only two arguments are provided, m is used for both row and column replication.

Examples

Replicate a scalar value into a 3x3 matrix

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

assertEquals(repmat(10, 3), [
  [10, 10, 10],
  [10, 10, 10],
  [10, 10, 10]
]);

Replicate a scalar into a 3x2 matrix

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

assertEquals(repmat(0.5, 3, 2), [
  [0.5, 0.5],
  [0.5, 0.5],
  [0.5, 0.5]
]);

Replicate a row vector

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

assertEquals(repmat([5, 6, 3], 1, 2), [
  [5, 6, 3, 5, 6, 3]
]);

Replicate a 2x2 matrix

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

assertEquals(repmat([[9, 5], [6, 1]], 2), [
  [9, 5, 9, 5],
  [6, 1, 6, 1],
  [9, 5, 9, 5],
  [6, 1, 6, 1]
]);

Replicate a column vector

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

assertEquals(repmat([[2], [3]], 2, 3), [
  [2, 2, 2],
  [3, 3, 3],
  [2, 2, 2],
  [3, 3, 3]
]);

Single-element matrix repeated

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

assertEquals(repmat([[7]], 2, 2), [
  [7, 7],
  [7, 7]
]);

Boolean replication

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

assertEquals(repmat(true, 2, 2), [
  [true, true],
  [true, true]
]);

Replicating a single-element array

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

assertEquals(repmat([4], 3, 2), [
  [4, 4],
  [4, 4],
  [4, 4]
]);

Type Parameters

Parameters

The value, array, or matrix to replicate.

m: number

Number of times to repeat along the rows.

optional
n: number

Number of times to repeat along the columns. Defaults to the value of m when omitted.

Return Type

The resulting replicated matrix.

Throws

Error If m or n are non-positive.

repmat<T>(
x: array<T>,
m: number,
n?: number,
): matrix<T>

Replicate and tile an array or matrix.

Creates a new matrix by repeating the input value, array, or matrix in a tiled fashion. If only two arguments are provided, m is used for both row and column replication.

Examples

Replicate a scalar value into a 3x3 matrix

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

assertEquals(repmat(10, 3), [
  [10, 10, 10],
  [10, 10, 10],
  [10, 10, 10]
]);

Replicate a scalar into a 3x2 matrix

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

assertEquals(repmat(0.5, 3, 2), [
  [0.5, 0.5],
  [0.5, 0.5],
  [0.5, 0.5]
]);

Replicate a row vector

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

assertEquals(repmat([5, 6, 3], 1, 2), [
  [5, 6, 3, 5, 6, 3]
]);

Replicate a 2x2 matrix

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

assertEquals(repmat([[9, 5], [6, 1]], 2), [
  [9, 5, 9, 5],
  [6, 1, 6, 1],
  [9, 5, 9, 5],
  [6, 1, 6, 1]
]);

Replicate a column vector

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

assertEquals(repmat([[2], [3]], 2, 3), [
  [2, 2, 2],
  [3, 3, 3],
  [2, 2, 2],
  [3, 3, 3]
]);

Single-element matrix repeated

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

assertEquals(repmat([[7]], 2, 2), [
  [7, 7],
  [7, 7]
]);

Boolean replication

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

assertEquals(repmat(true, 2, 2), [
  [true, true],
  [true, true]
]);

Replicating a single-element array

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

assertEquals(repmat([4], 3, 2), [
  [4, 4],
  [4, 4],
  [4, 4]
]);

Type Parameters

Parameters

The value, array, or matrix to replicate.

m: number

Number of times to repeat along the rows.

optional
n: number

Number of times to repeat along the columns. Defaults to the value of m when omitted.

Return Type

The resulting replicated matrix.

Throws

Error If m or n are non-positive.

repmat<T>(
x: T,
m: number,
n?: number,
): matrix<T>

Replicate and tile an array or matrix.

Creates a new matrix by repeating the input value, array, or matrix in a tiled fashion. If only two arguments are provided, m is used for both row and column replication.

Examples

Replicate a scalar value into a 3x3 matrix

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

assertEquals(repmat(10, 3), [
  [10, 10, 10],
  [10, 10, 10],
  [10, 10, 10]
]);

Replicate a scalar into a 3x2 matrix

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

assertEquals(repmat(0.5, 3, 2), [
  [0.5, 0.5],
  [0.5, 0.5],
  [0.5, 0.5]
]);

Replicate a row vector

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

assertEquals(repmat([5, 6, 3], 1, 2), [
  [5, 6, 3, 5, 6, 3]
]);

Replicate a 2x2 matrix

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

assertEquals(repmat([[9, 5], [6, 1]], 2), [
  [9, 5, 9, 5],
  [6, 1, 6, 1],
  [9, 5, 9, 5],
  [6, 1, 6, 1]
]);

Replicate a column vector

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

assertEquals(repmat([[2], [3]], 2, 3), [
  [2, 2, 2],
  [3, 3, 3],
  [2, 2, 2],
  [3, 3, 3]
]);

Single-element matrix repeated

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

assertEquals(repmat([[7]], 2, 2), [
  [7, 7],
  [7, 7]
]);

Boolean replication

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

assertEquals(repmat(true, 2, 2), [
  [true, true],
  [true, true]
]);

Replicating a single-element array

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

assertEquals(repmat([4], 3, 2), [
  [4, 4],
  [4, 4],
  [4, 4]
]);

Type Parameters

Parameters

x: T

The value, array, or matrix to replicate.

m: number

Number of times to repeat along the rows.

optional
n: number

Number of times to repeat along the columns. Defaults to the value of m when omitted.

Return Type

The resulting replicated matrix.

Throws

Error If m or n are non-positive.