How do I transpose a matrix?
-
There is an array of any length, but the arrays inside it will always have the same length. For example
const arr1 = [['a', 'b'], ['1', '2'], ['+', '-']]
or
const arr2 = [['a', 'b', 'c'], ['1', '2', '3'], ['+', '-', '*']]
It is necessary to make the function accept this array arr1 or arr2 at the input, and the result is the following for arr1:
const result = [['a', '1', '+'], ['b', '2', '-']]
or respectively for arr2:
const result = [['a', '1', '+'], ['b', '2', '-'], ['c', '3', '*']]
JavaScript Adrian Payne, Nov 18, 2019 -
const transpose = matrix => [...Array(matrix[0].length)].map((_, i) => matrix.map(n => n[i]));
Anonymous -
const pivot = a=>a.reduce((c,e,i)=>(e.forEach((n,j)=>c[j][i]=n),c),a[0].map(z=>[]));
Eden Hawkins
2 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!