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.