3
I’m trying to build a method capable of filling an n-dimensional matrix in all positions, for example: I have a 2-dimensional matrix, where this matrix is 2x2 with only 2 dimensions in a Cartesian plane, for example.
I would like the same method to fill a matrix with 3 dimensions yet 2x2, however I would not like to put fixed in the code in the case of loop two-dimensional one inside the other and when it is three-dimensional 3 also one loop inside each other. I need to use the same multidimensional code, for example: 8 dimensions with a 2x2 matrix.
My 2D example:
int matrix2d [] [] = new int [2] [2];
for (int i = 0; i <matrix2d.length; i ++) {
for (int j = 0; j <matrix2d [i] .length; j ++) {
System.out.println ("(" + i + "," + j + ")");
}
}
Result: (0.0) (0.1) (1.0) (1,1)
My 3D example:
int matrix3d [] [] [] = new int [2] [2] [2];
for (int i = 0; i <matrix3d.length; i ++) {
for (int j = 0; j <matrix3d [i] .length; j ++) {
for (int l = 0; l <matrix3d [i] [j] .length; l ++) {
System.out.println ("(" + i + "," + j + "," + l + "));
}
}
}
Result: (0.0.0) (0.0.1) (0.1.0) (0.1,1) (1.0.0) (1.0,1) (1,1,0) (1,1,1)
I would like to have a method or loop generic, not needing to include a loop each time the size increases.
Have you tried linearizing your matrix?
– Jefferson Quesado
https://stackoverflow.com/a/14773536/5524514
– user28595
I’ve done it, more would need as the posted indices of the results.
– Anderson Oliveira