There is some way to get the columns of one matrix per column in two repetitive cycles, that is, if I have the matrix:
A =
[21.0][16.0][16.0][13.0][11.0]
[17.0][18.0][14.0][23.0][13.0]
[32.0][27.0][15.0][41.0][19.0]
[6.0] [10.0][12.0][15.0][43.0]
Access to the elements is done through two cycles, one for the rows and one for the columns, in code is:
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < A[i].length; j++) {
System.out.print("["+A[i][j]+"]");
}
System.out.println();
}
Access is done for each row, but if you want to get something like:
Columna 0
[21.0]
[17.0]
[32.0]
[6.0]
Columna 1
[16.0]
[18.0]
[27.0]
[10.0]
.
.
.
Columna N
I have not managed to do it.
Someone mentioned that it can be done by changing i by j when accessing the elements, something like this:
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < A[i].length; j++) {
System.out.print("["+A[j][i]+"]");
}
System.out.println();
}
However, in doing so, throws an exception since j is greater than the total rows so the index is not found in the matrix.
How could I get the expected result?