Search code examples
javamatrixdivide

Matrix -> blocks division


I want to load some matrix into my program and then I want to divide it into smaller blocks. What I want exactly can be seen on an image below:

http://postimg.org/image/aki19hjx9/ba463111/

In red squares are 3 examples of my "blocks" in which I would like to divide whole matrix. In this case each block should be (smaller) 3x3 matrix. I know how to load it into 2d array, but what should I do then?


Solution

  • int[][] bigMatrix = new int[9][9];
    // initialize bigMatrix
    
    int[][][] smallMatrices = new int[3][3][3];
    
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            for (int k = 0; k < 3; k++) {
                smallMatrices[i][j][k] = bigMatrix[3*i+j][3*i+k];
            }
        }
    }
    
    // The submatrices are now in smallMatrices[i], 0 <= i < 3