All examples

Matrix transpose, .

Rows become columns: a nested loop reindexing a 2D array.

matrix-transpose.js
function transpose(matrix) {
  const rows = matrix.length;
  const cols = matrix[0].length;
  const result = [];
  for (let c = 0; c < cols; c++) {
    result.push([]);
    for (let r = 0; r < rows; r++) {
      result[c].push(matrix[r][c]);
    }
  }
  return result;
}

transpose([[1, 2, 3], [4, 5, 6]]);

What to watch

Watch result[c][r] mirror matrix[r][c] cell by cell in the data view.

Reading matrix transposeon a page only gets you so far - code is a process, and the process happens at runtime where you can't normally see it. In NeonFlow, this exact program runs step by step: every call opens a frame, every branch picks a path, and every value moves through memory in front of you. That's the fastest way to actually understand how matrix transpose works.

Run matrix transpose yourself

Open NeonFlow, paste this code (or any of your own), and scrub through it one step at a time.

Open NeonFlow