【问题标题】:Java, Compare Small 2D Array to Each Possible Iteration within a larger 2D arrayJava,将小型 2D 数组与较大 2D 数组中的每个可能迭代进行比较
【发布时间】:2014-04-25 01:18:54
【问题描述】:

好的,所以我需要能够在一个大数组(比如 15x20 或 20x17)中搜索一个只有 1 和 0 的字段,以找到它们的布局最准确地反映了哪个 13x13 数字模板,以一种原始 OCR。我的问题是我需要在较大数组的每个可能迭代中移动一个 13x13 数组,以查看它与数字数组最匹配的位置。这是我尝试过的:

  public double compareMatrices(int[][] num,int[][] mat){
    double score=0;
    double highest=0;
    int n=0;
    for(;n+num.length<mat.length;n++){
        int[][] rows=new int[num.length][];
        int m=0;
        for(;m+num.length<mat[0].length;m++){
            for(int o=0;o<num.length;o++){
                rows[o]=Arrays.copyOfRange(mat[n+o],m,m+num.length);
            }
            int p=0;
            for(;p<rows.length;p++){
                int q=0;
                for(;q < rows[0].length;q++){
                    if((rows[p][q]==1)&&(num[p][q]==1)){
                        score+=1;
                    }
                    else if((num[p][q]==1)&&(rows[p][q]==0)){
                        score-=.25;
                    }
                    else if((num[p][q]==0)&&(rows[p][q]==0)){
                        score+=.25;
                    }
                }
            }
        }
        if(score>highest){
            highest=score;
            score=0;
        }
        else{
            score=0;
        }

    }
    return(highest);

我的问题是,这似乎只是一遍又一遍地重复相同的 13x13 块,而没有移动到不同的块,无论是向侧面还是向下。任何建议在这里都会有所帮助,我们说话时我正在拔头发。 编辑: 示例输入数字数组:

0000001000000
0000011000000
0000011000000
0000101000000
0000001000000
0000001000000
0000001000000
0000001000000
0000001000000
0000001000000
0000001000000
0000001000000
0000111110000

我想搜索:

0 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0
0 0 0 1 0 0 1 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1
0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
0 0 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 1 1 0 1 0 0 0 0 0 0 0 1 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0
0 0 1 0 0 0 0 0 0 1 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0
0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 0
0 1 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 1 1 0 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0

我想在第二个中搜索每个可能的 13 x 13 矩阵,将其与第一个进行比较,并根据它们的相似性返回一个分数,返回最高的。

【问题讨论】:

  • 如果可以的话,提供一个示例输入和输出。

标签: java arrays matrix multidimensional-array


【解决方案1】:

这里有一个很好的问题,忍不住试一试。此解决方案适用于我提供的人为示例数据。

public static double compareMatrices(int[][] num, int[][] mat)
{
    double score = 0;
    double highest = 0;
    for (int numX = 0; numX <= num.length - mat.length; numX++)
    {
        for (int numY = 0; numY <= num[0].length - mat[0].length; numY++)
        {
            score = 0;
            for (int matX = 0; matX < mat.length; matX++)
            {
                for (int matY = 0; matY < mat[0].length; matY++)
                {
                    if (num[numX + matX][numY + matY] == mat[matX][matY])
                        score++;
                }
            }

            if(score >highest)
                highest = score;
        }
    }
    return highest; //highest num of elements in array that match sub-array
}

样本数据:

int[][] now = { { 1, 1, 1, 0 }, 
              { 0, 0, 0, 1 }, 
              { 1, 1, 1, 1 } };

int[][] no = { { 1, 0 }, 
               { 0, 1 } };
System.out.println(compareMatrices(now, no));  

输出4.0,将右上角的子数组标识为完美匹配。

【讨论】:

  • 我已经添加了测试用例。而且我对子数组的位置和最高分的位置不太感兴趣,因为我正在将它与其他 9 个数组一起运行以查看它与哪一个最相似。因此,接近结尾的条件语句,如果它们都是 1,则分数增加 1,如果当前矩阵的位置为 0,但输入数字矩阵为 1,则减少 0.25,如果两者均为 0,则分数增加0.25 岁。
  • @ChuckDivebomb 是的,这就是我的工作。据我所知,您的评分系统在确定相似性方面过于复杂。如果它们匹配,则添加一个。如果没有,不要。返回的数字是最大匹配数。如果你真的希望我让它与你的得分一起工作,我可以做到,但这是一个非常微不足道的改变。
  • 我一直在尝试使用 num 的不同输入数组运行它,但无论我运行什么,我似乎总是获得相同的最高分数,无论输入什么。任何知道是什么让它这样做了吗?
  • @chuckdivebomb 除非这是一个绝妙的巧合,否则我不会。如果您可以发布您正在使用的确切数组(包括您如何输入它们,如果您不对其进行硬编码),我可以看看。
  • @ChuckDivebomb 实际上,根据您使用的阵列,我想我确切知道可能导致您的问题的原因。但是,我需要再次看到它们才能做出真正的评估。
猜你喜欢
  • 2018-08-06
  • 1970-01-01
  • 1970-01-01
  • 2016-09-09
  • 1970-01-01
  • 1970-01-01
  • 2017-10-08
  • 1970-01-01
  • 2013-05-07
相关资源
最近更新 更多