简体   繁体   English

将2D数组拆分为较小的相等大小的2D子数组

[英]Splitting a 2D array into smaller equally sized 2D subarrays

Basically what I am asking is given a square 2D array and a valid patch size (the size of the 2D subarrays) how would I go about doing this. 基本上,我要问的是一个正方形的2D数组和一个有效的补丁大小(2D子数组的大小),我将如何去做。 Ultimately I don't need to store the subarrays in any way, I just need to find the median of each subarray and store them in a 1D array. 最终,我不需要以任何方式存储子数组,我只需要找到每个子数组的中值并将它们存储在一维数组中即可。 The median and storing to new array are simple for me, I just can't figure out how to go about the original 2D array and splitting it properly. 中位数和存储到新数组对我来说很简单,我只是不知道如何处理原始2D数组并将其正确拆分。 I've attempted this several times and keep getting out of bounds errors. 我已经尝试了几次,并且不断越界错误。 I have a 4x4: 我有一个4x4:

[1,2,3,4] [2,3,4,1] [3,4,1,2] [4,1,2,3]

I need to split it like so 我需要像这样拆分

[1,2] [3,4] [2,3] [4,1]

[3,4] [1,2] [4,1] [2,3]

And then take the median of each and store them into a new 1D array. 然后取每个的中位数并将其存储到新的一维数组中。

EDIT: Solved, thanks for the help! 编辑:解决了,谢谢您的帮助!

You can use Arrays.copyOfRange(Object[] src, int from, int to) for this where: 您可以Arrays.copyOfRange(Object[] src, int from, int to)使用Arrays.copyOfRange(Object[] src, int from, int to)

src is the source 1D array src是源1D数组

from is the initial index of the range to be copied, inclusive. from是要复制的范围的初始索引,包括端点。

to is the final index of the range to be copied, exclusive. to是要复制的范围的最终索引(不包括)。

I don't prefer your code because it seems that it's time complexity is too high. 我不喜欢您的代码,因为似乎时间复杂度太高。

Try below code: 试试下面的代码:

public class Temp {
    public static void main(String[] args) {
        int[][] arr = { { 1,2,3,4 },
                        { 2,3,4,1 },
                        { 3,4,1,2 },
                        { 4,1,2,3 } };

        int patch = 2;

        splitToSubArrays(arr, patch);

    }

    static void splitToSubArrays(int arr[][], int patch) {
        for (int i = 0; i < arr[0].length; i++) {
            int to = patch;
            for (int from = 0; to <= arr.length;) {
                int a[] = Arrays.copyOfRange(arr[i], from, to);
                // instead of printing you can store in a separate array for later usage
                System.out.println(Arrays.toString(a));
                to += patch;
                from += patch;
            }
        }
    }

}

EDIT: NB: For n*n array if n%patch is not 0 ie dimension is not divisible by patch value then you would need to use proper if condition here int a[] = Arrays.copyOfRange(arr[i], from, to); 编辑:注意:对于n*n数组,如果n%patch不为0即维不能被patch值整除,则if条件int a[] = Arrays.copyOfRange(arr[i], from, to); to control the index bound. 控制索引范围。 Hope you are aware of this. 希望您知道这一点。

Output 输出量

[1, 2]
[3, 4]
[2, 3]
[4, 1]
[3, 4]
[1, 2]
[4, 1]
[2, 3]

I think something like this could do, altough i didn't test it, but It should give you a good idea of how to do this scan 我认为可以进行类似的操作,尽管我没有对其进行测试,但是它应该可以使您很好地了解如何进行此扫描

public int[] patchArray(int[][] img, int patch)
{
    int size = img.length * (img[0].length / patch) ;
    int[] pArray = new int[size];
    int[] tmp = new int[patch];

    for (int row_i = 0; row_i < img.length; row_i++)
    {
        for (int patch_start = 0; patch_start < img[i].length; patch_start += patch)
        {
            int x = 0;
            for (int patch_i = patch_start; patch_i < (patch_start + patch); patch_i++)
            {
                tmp[patch_i - patch_start] = img[row_i][patch_i];
            }

            calculateMedian(tmp);
        }
    }

    return pArray;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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