Here is the scenario:
// getMatrix() returns int[]. It is 1-d // I wish it was 2d. int[] mat = MyMatrix.getMatrix(); // get height and width of the matrix; int h = MyMatrix.height; int w = MyMatrix.width; // calculate the center index of the matrix int c = ... // need help here // manipulate the center element of the matrix. SomeClass.foo(mat[c]);
Example: suppose I have a 5 x 5 matrix:
* * * * * // index 0 to 4 * * * * * // index 5 to 9 * * * * * // index 10 to 14. * * * * * // index 15 to 19 * * * * * // index 20 to 24
If getMatrix() should have returned int[][] , the center coordinate of this matrix would be (2,2) 0-index. But since getMatrix() returns int[] , the center coordinate index c is 12 .
However, if the height or width of the matrix is โโeven, the central index can be one of its 2 or 4 centers, as shown in the 6 x 6 matrix:
* * * * * * * * * * * * * * @ @ * * * * @ @ * * * * * * * * * * * * * *
-> Center is any of the @ above.
How would I calculate the center index c matrix mxn ?
source share