Open In App

JavaScript Program to Find the Transpose

Last Updated : 26 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The transpose of a matrix in linear algebra is a new matrix that is produced by flipping its rows and columns. Most numerical and mathematical applications depend on this technique.

There are several approaches available to find the transpose of the matrix in JavaScript, which are as follows:

Nested for loops

This approach iterates through the original matrix using a nested for loop and constructs a new matrix with the transposed elements. It is easy to understand but can be less efficient for larger matrices.

Example: JavaScript program demonstrating matrix transposition by using nested loops to transpose rows and columns in a 2D matrix for enhanced understanding and simplicity.

Javascript




function transpose1(matrix) {
    const transposed = [];
    for (let i = 0; i < matrix[0].length; i++) {
        transposed.push([]);
        for (let j = 0; j < matrix.length; j++) {
            transposed[i].push(matrix[j][i]);
        }
    }
    return transposed;
}
const exampleMatrix = [
    [1, 2, 3],
    [4, 5, 6],
];
console.log(transpose1(exampleMatrix));


Output

[ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]

Map and Reduce

This approach utilizes higher-order functions like map and reduce for a more concise and potentially more readable solution.

Example: JavaScript program for transposing a matrix using functional programming by utilizing map functions to transpose rows and columns within a given 2D matrix.

Javascript




function transpose2(matrix) {
    return matrix[0].map((_, colIndex) =>
        matrix.map(row => row[colIndex])
    );
}
const exampleMatrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];
const transposedMatrix = transpose2(exampleMatrix);
console.log(transposedMatrix);


Output

[ [ 1, 4, 7 ], [ 2, 5, 8 ], [ 3, 6, 9 ] ]

Spread Operator and Reduce

In particular, for larger matrices, this method may be more efficient because it makes use of the spread operator and reduces. The reduce function accumulates the transposed matrix starting with an empty array now for each row in the original matrix, The spread operator (…) creates a copy of the current transposed array and the map function iterates through each element of the current row. It then retrieves the corresponding element from the same column index in each row of the original matrix using matrix[colIndex][row.indexOf(_)]. The mapped elements for the current row are then added to the transposed array. The final reduce call returns the accumulated to the transposed matrix.

Example: JavaScript program for transposing a matrix: A concise example demonstrating the transpose function applied to a 2D matrix to interchange rows and columns.

Javascript




const matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];
 
function transpose(matrix) {
    return matrix[0].map((_, i) => {
        return matrix.reduce((acc, row) => [...acc, row[i]], [])
    });
}
const transposedMatrix = transpose(matrix);
console.log(transposedMatrix);


Output

[ [ 1, 4, 7 ], [ 2, 5, 8 ], [ 3, 6, 9 ] ]


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads