DEV Community

Jessica Alves
Jessica Alves

Posted on • Edited on

Transpose of a matrix

Introduction

Studying an interview challenge related to converting a matrix to a symmetric matrix with minimum insert operations led me to another matrix challenge: matrix transpose. So I found the problem on LeetCode and decided to share my solution.

Problem

The problem is simple:

"Given a 2D integer array matrix, return the transpose of matrix."

Discussing a solution

According to the problem statement, "The transpose of a matrix is the matrix flipped over its main diagonal, switching the matrix's row and column indices."

Starting from that assumption what we're trying to do, in other words, is:
transposed[row][column] = matrix[column][row]

So for a 2D matrix there is an index for each row and each column, in that order: [row][column]. After switching the indexes of the original matrix, which will be [column][row], we assign it to its respective position in the new matrix transposed leading up to the expression written above.

For example, let's consider the input matrix = [[1, 2, 4], [5, 7, 8]]. In the first iteration of the solution to be presented we add a new array to transposed, which will be a new row in the transposed matrix. That means for the number of existing columns in the original matrix len(matrix[0]) we will add a new row. So at the end of the first for loop we have this:
transposed = [[]].

Then in the second for loop we add the element matrix[column][row] to the first row in the transposed matrix. This element comes from the first row and first column of the original matrix: matrix[0][0]. So we're adding the element 1 to transposed, which is now transposed = [[1]]. After that our code goes to the second iteration in the second for loop and the element to be added will be matrix[0][1], which is 5. So the new matrix becomes transposed = [[1, 5]]. The process is repeated for every column of the original matrix until we get the final result of the transposed matrix, which will be transposed = [[1, 5], [2, 7], [4, 8]].

Solution

Here is a Python solution based on the logic discussed above:

def transpose(matrix):
    transposed = []
    for row in range(len(matrix[0])):
        transposed.append([])
        for column in range(len(matrix)):
            transposed[row].append(matrix[column][row])
    return transposed

# or a one line solution following the same logic but removing creating a new array:
def transpose(matrix):
    return [[matrix[column][row] for column in range(len(matrix))] for row in range(len(matrix[0]))]
Enter fullscreen mode Exit fullscreen mode

And the same solution in JavaScript:

const transpose = (matrix) => {
    const transposed = [];
    for (const row in matrix[0]) {
        transposed.push([]);
        for (const column of matrix) {
            transposed[row].push(column[row]);
        }
    }
    return transposed;
};
Enter fullscreen mode Exit fullscreen mode

Considerations

For more details on the solution such as time and space complexity, check out my contribution to LeetCode The Hard Way.

Top comments (0)