HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Transpose a matrix in JavaScript

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
javascriptmatrixtranspose

Problem

A matrix transpose is an operation that switches the rows and columns of a two-dimensional array. In other words, it converts the rows of the matrix into columns and vice versa.
In order to perform this operation in JavaScript, we can use Array.prototype.map() to create a new array where each column of the original matrix becomes a row in the transposed matrix. We can achieve this by mapping over the columns of the original matrix and creating a new row by mapping over the rows and selecting the corresponding element.
@Further reading

Solution

const transpose = arr => arr[0].map((col, i) => arr.map(row => row[i]));

transpose([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]);
// [[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]


@Further reading

Code Snippets

const transpose = arr => arr[0].map((col, i) => arr.map(row => row[i]));

transpose([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]);
// [[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]

Context

From 30-seconds-of-code: transpose-matrix

Revisions (0)

No revisions yet.