trekhleb/javascript-algorithms · error · Error

Matrix is not of 2D shape

Error message

Matrix is not of 2D shape

What it means

Error "Matrix is not of 2D shape" thrown in trekhleb/javascript-algorithms.

Source

Thrown at src/algorithms/math/matrix/Matrix.js:50

const validate2D = (m) => {
  validateType(m);
  const aShape = shape(m);
  if (aShape.length !== 2) {
    throw new Error('Matrix is not of 2D shape');
  }
};

View on GitHub (pinned to 85293e3e2b)

Solutions

  1. Pass a 2D matrix (array of row arrays) instead of a 1D vector or a 3D+ tensor.
  2. Reshape or wrap the data as [[...], [...]] before calling dot or t.
  3. Check shape(m).length === 2 before invoking operations restricted to 2D matrices.

When it happens

Trigger: A 2D-only matrix operation (validate2D, e.g. via dot) receives a matrix whose computed shape length is not 2 — a flat 1D array or a 3D-or-higher nested array.

Common situations: Calling matrix multiplication or transpose with a vector or higher-dimensional matrix where a strictly 2D matrix is required.


AI-assisted analysis of trekhleb/javascript-algorithms@85293e3e2b (2026-08-24). Data as JSON: /api/errors/e053ce6c2ecfde6f. Report an issue: GitHub.