H
H
heySasha2017-03-22 14:48:32
JavaScript
heySasha, 2017-03-22 14:48:32

How to transpose a matrix without using cycles?

Hey! Who knows how to transpose a matrix without using cycles? Using map, reduce, ... For example [ [1, 2], [3, 4], [5, 6] ], the output is [ [1, 3, 5], [2, 4, 6] ].

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton, 2017-03-22
@heySasha

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

const baseMatrix = ;
const transposedMatrix = transpose(baseMatrix);
// [ [ 1, 3, 5 ], [ 2, 4, 6 ] ]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question