/**
* see the matrix in "columns".
* @example
* let m = [[1,2], [3], [5,6]];
* m.transpose(); // [[1,3,5], [2,undefined,6]]
* @return {[[*]]} array of "columns"
*/
const matrix_transpose = {
/**
* see the matrix in "columns".
* - Note: may contain `undefined` elements if row lengths are different
* @example
* let m = [[1,2], [3], [5,6]];
* m.transpose(); // [[1,3,5], [2,undefined,6]]
* @return {[[*]]} array of "columns"
*/
transpose() {
return Array.from(
{ length: Math.max(...this.map(row => row.length)) }, // number of columns
(_, j) => this.map(row => row[j]) // for each row, choose jth column
)
}
};
export default matrix_transpose;