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

Set a column of a matrix.

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

Examples

Replace the first column of a matrix

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

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

Replace the third column of a matrix

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

assertEquals(setcol([9, 21], [[5, 6, 5], [7, 8, -1]], 2), [
  [5, 6, 9],
  [7, 8, 21]
]);

Column vector length mismatch error

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

assertThrows(() => setcol([1, 2, 3], [[4, 5], [6, 7]], 1), "Column vector length must match the number of matrix rows.");

Column index out of bounds error

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

assertThrows(() => setcol([1, 2], [[4, 5], [6, 7]], 2), "Column index must be an integer between 0 and N-1.");

Parameters

Column vector (Mx1) to insert.

Matrix (MxN) in which to set the column.

n: number

Column index (0-based).

Return Type

A new matrix with the updated column.

Throws

When the column index is out of bounds or the vector length mismatches the number of rows.