function setrow
setrow(
x: array,
mat: matrix,
n: number,
): matrix

Set a row of a matrix.

Replaces the values of row n in a matrix with a given row vector.

Examples

Replace the first row of a matrix

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

assertEquals(setrow([2, 0, -2], [[5, 6, 5], [7, 8, -1]], 0), [
  [2, 0, -2],
  [7, 8, -1]
]);

Replace the second row of a matrix

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

assertEquals(setrow([9, 21, 57], [[5, 6, 5], [7, 8, -1]], 1), [
  [5, 6, 5],
  [9, 21, 57]
]);

Row vector length mismatch error

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

assertThrows(() => setrow([1, 2], [[4, 5, 6], [7, 8, 9]], 1), "Row vector length must match the number of matrix columns.");

Row index out of bounds error

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

assertThrows(() => setrow([1, 2, 3], [[4, 5, 6], [7, 8, 9]], 2), "Row index must be an integer between 0 and M-1.");

Parameters

Row vector (1xN) to insert.

Matrix (MxN) in which to set the row.

n: number

Row index (0-based).

Return Type

A new matrix with the updated row.

Throws

When the row index is out of bounds or the vector length mismatches the number of columns.