transpose of a matrix java

htt‮/:sp‬/www.lautturi.com
transpose of a matrix java

To transpose a matrix in Java, you can use a nested loop to iterate over the rows and columns of the matrix and create a new matrix with the rows and columns reversed.

Here is an example of how you could transpose a matrix represented as a two-dimensional array:

public static int[][] transpose(int[][] matrix) {
    int rows = matrix.length;
    int cols = matrix[0].length;
    int[][] transposed = new int[cols][rows];

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            transposed[j][i] = matrix[i][j];
        }
    }

    return transposed;
}

This function takes in a matrix represented as a two-dimensional array, and returns a new matrix that is the transpose of the original matrix.

Created Time:2017-10-17 20:18:57  Author:lautturi