简体   繁体   中英

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.

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. Also note "maxCount" is set to 0 outside the loop where count = 0 is. 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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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