Jamsir Crown
Jamsir Crown

Reputation: 49

Splitting a Java 2D array (4x4) into smaller ones (2x2) and joining them again

This source code splits a 4x4 array into an array of 2x2... How can I restore the value of the 2x2 array into a 4x4 array?

public class testmatriks {
    private static Random generator = new Random();

    public static void main(String[] args) {
        double[][] mainArray = new double[4][4];
        double[][] subArray = new double[2][2];
        double[][] rubArray = new double[4][4];
        int value;
        for (int i = 0; i < mainArray.length; i++) {
            for (int j = 0; j < mainArray[0].length; j++) {
                value = generator.nextInt(255);
                mainArray[i][j] = value;
                System.out.print(mainArray[i][j] + "  ");
            }
            System.out.println("");
        }
        System.out.println("\n");
        System.out.println("pecah ke piksel 2x2 piksel");

        for (int row = 0; row < (mainArray.length); row += 2) {
            for (int column = 0; column < mainArray[0].length; column += 2) {
                for (int k = 0; k < 2; k++) {
                    for (int l = 0; l < 2; l++) {
                        subArray[k][l] = mainArray[row + k][column + l] * 2;
                        System.out.print(subArray[k][l] + " ");
                    }
                    System.out.println(" ");
                }
            }
        }
    }
}

This picture illustrates what I want

This picture illustrates what I want.

Upvotes: 2

Views: 3830

Answers (1)

smac89
smac89

Reputation: 43186

This is what you initially have in the 4x4 array:

[a1| b1| c1 | d1]
[a2| b2| c2 | d2]
[a3| b3| c3 | d3]
[a4| b4| c4 | d4]

This is what will be in the 2x2 array you created:

[2 * c3| 2 * d3]
[2 * c4| 2 * d4]

When you say to "restore", it is not clear where to place the values in this 4x4 array, so I will just go with the places in the 4x4 used to create the 2x2:

The code:

for (int r = 0; r < 2; r++) {
    for (int c = 0; c < 2; c++) {
        mainArray[r + 2][c + 2] = subArray[r][c];
    }
}

This should be the content after the code runs:

[a1| b1|    c1     |    d1     ]
[a2| b2|    c2     |    d2     ]
[a3| b3| c3 + c3| d3 + d3]
[a4| b4| c4 + c4| d4 + d4]


For splitting the array into smaller chunks, here is what I came up with:

/**
* @param larger the larger array to be split into sub arrays of size chunksize
* @param chunksize the size of each sub array
* @precond chunksize > 0 && larger != null
* @throws ArrayIndexOutOfBoundsException, NullPointerException
*/
public static List<double[][]> chunks(double [][]larger, int chunksize) throws 
    ArrayIndexOutOfBoundsException, NullPointerException  {
    if (chunksize <= 0)
        throw new ArrayIndexOutOfBoundsException("Chunks must be atleast 1x1");
    int size = larger.length / chunksize * (larger[0].length / chunksize);
    List<double[][]> subArrays = new ArrayList<>();

    for (int c = 0; c < size; c++) {
        double[][] sub = new double[chunksize][chunksize];
        int startx = (chunksize * (c / chunksize)) % larger.length;
        int starty = (chunksize * c) % larger[0].length;

        if (starty + chunksize > larger[0].length) {
            starty = 0;
        }

        for (int row = 0; row < chunksize; row++) {
            for (int col = 0; col < chunksize; col++) {
                sub[row][col] = larger[startx + row][col + starty];
            }
        }
        subArrays.add(sub);
    }

    return subArrays;
}

I haven't tested it yet, but you can easily test it with the code you already have.

Sample usage: http://ideone.com/D5Tdgs

Upvotes: 3

Related Questions