Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Transpose an array of arrays using matrix transposition.
- * The array is returned, *NOT* transposed in place.
- *
- * Handles matrixes where the number of rows != number of columns.
- * The number of columns in each row must be the same.
- * E.g.
- [ | [
- [1,2,3], | [1,2],
- [4,5,6], | [3,4],
- [7,8,9], | [5,6],
- ] | ]
- becomes | becomes
- [ | [
- [1,4,7], | [1,3,5],
- [2,5,8], | [2,4,6],
- [3,6,9], | ]
- ] |
- * @param {Array} array The array to transpose.
- * @return {Array}
- */
- function transpose( array ) {
- let newArray = [];
- for (let i = 0, len = array[0].length; i < len; i++) {
- newArray.push( [] );
- for (let j = 0, len = array.length; j < len; j++) {
- newArray[i].push( array[j][i] );
- }
- }
- return newArray;
- }
Add Comment
Please, Sign In to add comment