-
Notifications
You must be signed in to change notification settings - Fork 590
/
Copy pathMatrixTranspose.java
36 lines (31 loc) · 1.2 KB
/
MatrixTranspose.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class MatrixTranspose
{
public static void main(String[] args) {
int rows, cols;
//Initialize matrix a
int a[][] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
//Calculates number of rows and columns present in given matrix
rows = a.length;
cols = a[0].length;
//Declare array t with reverse dimensions
int t[][] = new int[cols][rows];
//Calculates transpose of given matrix
for(int i = 0; i < cols; i++){
for(int j = 0; j < rows; j++){
//Converts the row of original matrix into column of transposed matrix
t[i][j] = a[j][i];
}
}
System.out.println("Transpose of given matrix: ");
for(int i = 0; i < cols; i++){
for(int j = 0; j < rows; j++){
System.out.print(t[i][j] + " ");
}
System.out.println();
}
}
}