简体   繁体   English

在二维数组中寻找相似性

[英]Finding Similarity Within 2-Dimensional Array

I have a 2-dimensional array and I need to compare the arrays within the array to find similarities between them. 我有一个二维数组,我需要比较数组中的数组以查找它们之间的相似性。 If one item is found in one array and another it will add one to the count. 如果在一个数组中找到一个项目,在另一个数组中找到一个项目,它将增加一个计数。 Count keeps track of similarities. 计数跟踪相似性。 If the count is the highest so far then it takes that one as the most similar. 如果计数是目前为止最高的,则认为该计数是最相似的。 It will then print Blank is most similar to blank. 然后将打印空白与空白最相似。

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);
            }

This is the general idea behind what needs to be done. 这是需要执行的操作的基本思路。 However I'm strugling with getting the proper result and I cannot figure it out. 但是,我正在努力获得正确的结果,我无法弄清楚。 The result I am getting from this code is: 我从这段代码中得到的结果是:

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

And the result I need is: 我需要的结果是:

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

The problem with your code is that you reset the count and the maxCount at the same time and also when you increment count and immediately set maxCount = count, it causes maxCount to be almost always the same as count. 代码的问题在于,您同时重置计数和maxCount,并且在递增计数并立即设置maxCount = count时,它会使maxCount几乎总是与count相同。

Check out the following code and the results below: 查看以下代码和以下结果:

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);
}

Note "count" is set to 0 right before the start of the k loop, and the comparison happens right after. 注意,“计数”在k循环开始之前设置为0,然后立即进行比较。 Also note "maxCount" is set to 0 outside the loop where count = 0 is. 另请注意,在count = 0所在的循环外部,“ maxCount”设置为0。 This will return the following results, which are valid: 这将返回以下有效的结果:

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