How to display transpose of a matrix in Node.js

1 Answer

0 votes
function displayTransposeMatrix(matrix) {
    const rows = matrix.length;
    const cols = matrix[0].length;
 
    for (let i = 0; i < cols; i++) {
        let s = "";
        for (let j = 0; j < rows; j++) {
             s += matrix[j][i] + " ";
        }
        console.log(s);
    }
}
 
const matrix = [  [9, 0, 7, 1],
                  [4, 5, 6, 3],
                  [2, 8, 4, 0] ];

displayTransposeMatrix(matrix); 
  

 
   
     
     
/*
run:
     
9 4 2 
0 5 8 
7 6 4 
1 3 0 
     
*/

 



answered Jul 6, 2022 by avibootz

Related questions

1 answer 128 views
1 answer 130 views
1 answer 179 views
1 answer 182 views
1 answer 305 views
...