Skip to content

Instantly share code, notes, and snippets.

@formigone
Created July 20, 2017 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save formigone/6614e7937ce0d25dfa3dbcbb4b44efcf to your computer and use it in GitHub Desktop.
Save formigone/6614e7937ce0d25dfa3dbcbb4b44efcf to your computer and use it in GitHub Desktop.
Computes the transpose of a matrix represented by a 2-dimensional array
function transpose(mat){
return mat[0].map((_, y) => {
return mat.map((_, x) => {
return mat[x][y];
});
});
}
var mat0 = [
[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9],
];
transpose(mat0);
// [
// [0, 5],
// [1, 6],
// [2, 7],
// [3, 8],
// [4, 9],
// ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment