How to display transpose of a matrix in JavaScript

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 = [  [1, 2, 3, 5],
                  [4, 5, 6, 1],
                  [7, 8, 9, 0] ];

displayTransposeMatrix(matrix); 
  

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

 



answered Jul 6, 2022 by avibootz

Related questions

1 answer 179 views
1 answer 161 views
1 answer 305 views
1 answer 228 views
228 views asked Jan 13, 2022 by avibootz
1 answer 310 views
...