Java,将小型二维数组与较大二维数组中的每个可能的迭代进行比较

标签 java arrays matrix multidimensional-array

好吧,所以我需要能够搜索一个大数组(例如 15x20 或 20x17),其中除了 1 和 0 之外什么都没有,以找到其布局最准确反射(reflect)的 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 block ,而没有移动到不同的 block ,无论是向一侧还是向下。任何建议都会有帮助,在我们说话的时候我正在抓狂。 编辑: 输入数字数组示例:

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 矩阵,将其与第一个矩阵进行比较,并根据它们的相似性返回一个分数,返回最高的分数。

最佳答案

这里有一个很好的问题,忍不住尝试一下。该解决方案适用于我提供的设计的示例数据。

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,将右上角的子数组识别为完美匹配。

关于Java,将小型二维数组与较大二维数组中的每个可能的迭代进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22494966/

相关文章:

java - 通过tomcat在默认打印机上打印

java - 如何在java中迭代数组[...][i](列)

c - 如何计算矩阵中不同元素的数量? C语言编程

java - 添加 ScrollView

java - 当线程阻塞时,在线程上调用的中断不会抛出中断异常

java - 将 JSON 保存到文件时出现编码问题,如何另存为 UTF-8?

arrays - 将 Long 转换为 Array Byte 的 Scala 方法

c - 在 C 中使用#include <stdbool.h>

Java 按位运算符未按预期工作

Python 连接和堆叠多个矩阵