繁体   English   中英

在二维数组中寻找相似性

[英]Finding Similarity Within 2-Dimensional Array

我有一个二维数组,我需要比较数组中的数组以查找它们之间的相似性。 如果在一个数组中找到一个项目,在另一个数组中找到一个项目,它将增加一个计数。 计数跟踪相似性。 如果计数是目前为止最高的,则认为该计数是最相似的。 然后将打印空白与空白最相似。

double[][] ratingDB = {{4.0, 3.0, 3.0, 3.0, 3.0}, {3.0, 2.0, 3.0, 3.5, 3.0}, 
           {4.0, 3.0, 4.0, 3.0, 3.0}, {3.0, 2.0, 3.0, 3.5, 3.0}, {3.0, 2.0, 3.0, 3.5, 3.0}};
String temp = null;

            for (int i = 0; i < ratingDB.length; i++) {
                for (int j = 1; j < ratingDB.length; j++) {
                            int maxCount = 0;
                            int count = 0;
                    for (int k = 0; k < ratingDB.length-1; k++) {
                        if (ratingDB[i][k] == ratingDB[j][k]) {
                            count++;
                            if (count >= maxCount) {
                                maxCount = count;
                                temp = "User_" + k;
                            }
                        }
                    }
                }
                System.out.println("User_" + i + " is most simlar to " + temp);
            }

这是需要执行的操作的基本思路。 但是,我正在努力获得正确的结果,我无法弄清楚。 我从这段代码中得到的结果是:

User_0 is most simlar to User_2
User_1 is most simlar to User_3
User_2 is most simlar to User_3
User_3 is most simlar to User_3
User_4 is most simlar to User_3

我需要的结果是:

user_0 most similar to user_2
user_1 most similar to user_4
user_2 most similar to user_0
user_3 most similar to user_4
user_4 most similar to user_3

代码的问题在于,您同时重置计数和maxCount,并且在递增计数并立即设置maxCount = count时,它会使maxCount几乎总是与count相同。

查看以下代码和以下结果:

double[][] ratingDB = {
        {4.0, 3.0, 3.0, 3.0, 3.0},
        {3.0, 2.0, 3.0, 3.5, 3.0},
        {4.0, 3.0, 4.0, 3.0, 3.0},
        {3.0, 2.0, 3.0, 3.5, 3.0},
        {3.0, 2.0, 3.0, 3.5, 3.0}};

int height = ratingDB.length;
int width = ratingDB[0].length;;
for (int i = 0; i < height; i++) {
    int maxCount = 0;
    int temp = -1;
    for (int j = 0; j < height; j++) {
        int count = 0;
        for (int k = 0; k < width; k++) {
            if (ratingDB[i][k] == ratingDB[j][k] && i != j) {
                count++;
            }
        }
        if (count > maxCount) {
            maxCount = count;
            temp = j;
        }
     }

    System.out.println("User_" + i + " is most similar to User_" + temp);
}

注意,“计数”在k循环开始之前设置为0,然后立即进行比较。 另请注意,在count = 0所在的循环外部,“ maxCount”设置为0。 这将返回以下有效的结果:

User_0 is most similar to User_2
User_1 is most similar to User_3
User_2 is most similar to User_0
User_3 is most similar to User_1
User_4 is most similar to User_1
import java.util.Arrays;

int[][] arr1;
int[][] arr2;
//...
if (Arrays.deepEquals(arr1, arr2))

暂无
暂无

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

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