简体   繁体   中英

dividing matrix into four sub-blocks

i want devide matrix into four sub-blocks equally by vertically and horizontallty in java (Here, we suppose that m and nare even numbers) .

for example we have matrix:

1 2 3 4 5 6                  
7 8 9 1 2 8
1 2 3 4 5 6
4 5 6 7 8 9
1 4 7 2 5 8
3 6 9 7 2 5

I want to display the last block that is:

7 8 9
2 5 8
7 2 5

how i can resolve this problem in java.

Iterate over the lower-right part of the matrix. Here is an example for a square matrix. I am sure you will be able to make it more generic for non-square quadrants or to get other quadrants than the lower-right one.

public int[][] getQuadrantOfSquareMatrix(int[][] matrix) {
    int newDimension = matrix.length / 2;
    int[][] toReturn = new int[newDimension][newDimension];
    for (int i = 0; i < newDimension; i++) {
        for (int j = 0; j < newDimension; j++) {
            toReturn[i][j] = matrix[i + newDimension][j + newDimension];
        }
    }
    return toReturn;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM