Home > OS >  how to split a 2d array into multiple 2d arrays of different sized in java
how to split a 2d array into multiple 2d arrays of different sized in java

Time:12-12

I have a 6*6 2d array called pixels

  int [][] pixels = { {1,2,7,9,4,11},
                    {3,4,6,6,12,12},
                    {4,9,15,14,9,9},
                    {10,10,20,18,8,8},
                    {4,3,17,16,1,4},
                    {4,5,18,18,5,6}
            };

and I want to store its data into 9 2d arrays of size 2*2 like this

 int [][] array1 = {{1,2},
            {3,4}
    };

    int [][] array2 = {{7,9},
            {6,6}
    };

    int [][] array3 = {{4,11},
            {12,12}
    };

and like this, I have another 2d array of size 320 * 320 which I want to store its data in 6400 2d arrays of size 44 and I don't know what is the dynamic to do. my for loops aren't right. Can anyone help? edit: I tried this code and it gives me the right result regrading the first array of the 2d array but I'm not sure about the second one. vectorHeight is 4. the big 2d array size is 320 320 and the smaller ones size is 4 * 4

for(int j=0; j < 320  ; j  ){
            for(int i=0; i< 320 ; i  ){
                int[][] array = new int[][]{
                        {pixels2[j * vectorHeight][i * vectorHeight], pixels2[j * vectorHeight][(i * vectorHeight)   1],pixels2[j * vectorHeight][(i * vectorHeight)   2],pixels2[j * vectorHeight][(i * vectorHeight)   3]},
                        {pixels2[(j * vectorHeight)   1][i * vectorHeight], pixels2[(j * vectorHeight)   1][(i * vectorHeight)   1],pixels2[(j * vectorHeight)   2][(i * vectorHeight)   2],pixels2[(j * vectorHeight)   3][(i * vectorHeight)   3]},
                };
                         System.out.println(Arrays.deepToString(array));
                }
            }

edit2: the 320 * 320 2d array is a result of a function that takes an image and return the pixels vector so this is the code for reading an image:

public static int[][] readImage(String filePath) {
        File file = new File(filePath);
        BufferedImage image = null;
        try {
            image = ImageIO.read(file);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        int width = image.getWidth();
        int height = image.getHeight();
        int[][] pixels = new int[height][width];
        for (int x = 0; x < width; x  ) {
            for (int y = 0; y < height; y  ) {
                int p = image.getRGB(x, y);
                int a = (p >> 24) & 0xff;
                int r = (p >> 16) & 0xff;
                int g = (p >> 8) & 0xff;
                int b = p & 0xff;

                pixels[y][x] = r;

                p = (a << 24) | (r << 16) | (g << 8) | b;
                image.setRGB(x, y, p);
            }

        }

        return pixels;
    }

and the image that I am currently working on is this one https://www.researchgate.net/profile/Ayman-Al-Dmour/publication/304496637/figure/fig1/AS:377628822392833@1467045135613/Test-images_Q320.jpg the rest of the code is a function that takes an image, a vector size - height and weight- and eventually its suppose to compress it using vector quantization

int [][] pixels = readImage(filePath);
int numofVectors1 = (pixels.length * pixels.length) / (vectorHeight * vectorWidth);
        ArrayList<int[][]> vectors = new ArrayList<>(numofVectors1);
         for (int j = 0; j < pixels.length; j  ) {
                for (int i = 0; i < pixels.length; i  ) {
                    int[][] array = new int[][]{
                            {pixels[j * vectorHeight][i * vectorHeight], pixels[j * vectorHeight][(i * vectorHeight)   1], pixels[j * vectorHeight][(i * vectorHeight)   2], pixels[j * vectorHeight][(i * vectorHeight)   3]},
                            {pixels[(j * vectorHeight)   1][i * vectorHeight], pixels[(j * vectorHeight)   1][(i * vectorHeight)   1], pixels[(j * vectorHeight)   1][(i * vectorHeight)   2], pixels[(j * vectorHeight)   1][(i * vectorHeight)   3]},
                    };
                    vectors.add(array);
                }

        }

CodePudding user response:

In your case it's helpful to write down what you want:

         [0][0] [0][1] [1][0] [1][1]
         [0][2] [0][3] [1][2] [1][3]
         [0][4] [0][5] [1][4] [1][5]

         [2][0] [2][1] [3][0] [3][1]
         [2][2] [2][3] [3][2] [3][3]
         [2][4] [2][5] [3][4] [3][5]

         [4][0] [4][1] [5][0] [5][1]
         [4][2] [4][3] [5][2] [5][3]
         [4][4] [4][5] [5][4] [5][5]

Maybe you can already see a pattern here. First, I created the inner loop for the first three lines:

for (int i = 0; i < 3; i  ) {
    int[][] array = new int[][]{
            {pixels[0][i * 2], pixels[0][(i * 2)   1]},
            {pixels[1][i * 2], pixels[1][(i * 2)   1]}
    };
    System.out.println(Arrays.deepToString(array));
}

As you see, it's always the zeroth and first row for the first three lines. And in the next three lines, it's always the second and thrird row. And the rule for all columns is that it's always zero, two, four and one, three, five.

So it comes to the nested loop:

for (int j = 0; j < 3; j  ) {
    for (int i = 0; i < 3; i  ) {
        int[][] array = new int[][]{
            {pixels[j * 2][i * 2], pixels[j * 2][(i * 2)   1]},
            {pixels[(j * 2)   1][i * 2], pixels[(j * 2)   1][(i * 2)   1]}};
        System.out.println(Arrays.deepToString(array));
    }
}

Now you can store the created arrays for example in a list.

EDIT

Sorry, I think I made a mistake. This should do what you want: create 6400 4x4 arrays. So you have to loop until the square root of the number of arrays you want(in this case 6400). Do you have a possibility to check if the arrays are correct now? Try this:

int vectorHeight = 4, vectorWidth = 4;
int numofVectors1 = (pixels.length * pixels.length) / (vectorHeight * vectorWidth);
ArrayList<int[][]> vectors = new ArrayList<>(numofVectors1);
int numberOfArrays = 6400;
        for (int j = 0; j < Math.sqrt(numberOfArrays); j  ) {
            for (int i = 0; i < Math.sqrt(numberOfArrays); i  ) {
                int[][] array = new int[][]{
                        {
                                pixels[j * vectorHeight][i * vectorHeight],
                                pixels[j * vectorHeight][(i * vectorHeight)   1],
                                pixels[j * vectorHeight][(i * vectorHeight)   2],
                                pixels[j * vectorHeight][(i * vectorHeight)   3]
                        },
                        {
                                pixels[(j * vectorHeight)   1][i * vectorHeight],
                                pixels[(j * vectorHeight)   1][(i * vectorHeight)   1],
                                pixels[(j * vectorHeight)   1][(i * vectorHeight)   2],
                                pixels[(j * vectorHeight)   1][(i * vectorHeight)   3]
                        },
                        {
                                pixels[(j * vectorHeight)   2][i * vectorHeight],
                                pixels[(j * vectorHeight)   2][(i * vectorHeight)   1],
                                pixels[(j * vectorHeight)   2][(i * vectorHeight)   2],
                                pixels[(j * vectorHeight)   2][(i * vectorHeight)   3]
                        },
                        {
                                pixels[(j * vectorHeight)   3][i * vectorHeight],
                                pixels[(j * vectorHeight)   3][(i * vectorHeight)   1],
                                pixels[(j * vectorHeight)   3][(i * vectorHeight)   2],
                                pixels[(j * vectorHeight)   3][(i * vectorHeight)   3]
                        },
                };
                vectors.add(array);
            }
        }

  • Related