简体   繁体   English

查找二维数组的模式

[英]Finding the mode of a 2D array

I'm trying to return the mode of a 2D array using a frequency array.我正在尝试使用频率数组返回二维数组的模式。 I have an array, score, which is of length 10, and has 3 columns.我有一个数组 score,长度为 10,有 3 列。 Each column contains an int that is between 0 and 100.每列包含一个介于 0 和 100 之间的 int。

I'm trying to find a way that will iterate through the array and return the modal value.我试图找到一种方法来遍历数组并返回模态值。 What I have so far is:到目前为止我所拥有的是:

    int value = 0;
    int[] freq = new int[100];

    for (int row = 0; row < score.length; row++) {
        for (int col = 0; col < score[row].length; col++) {
            score[row][col] = value;
            freq[value]++;
        }
    }
    int largest = 0;
    int mode = -1;

    for (int i = 0; i < 100; i++) {
        if (freq[i] > largest)
        {
            largest = freq[i];
            mode = i;
        }
    }
    System.out.println("modal score is: " +mode);

Problem is that this is just returning the modal score as 0, which it isn't.问题是这只是将模态分数返回为 0,而事实并非如此。

You have a problem on generating the freq array.您在生成freq数组时遇到问题。 If I understand correctly, on the first double-for block you are trying to put the frequencies of the numbers inside the freq array.如果我理解正确,在第一个 double-for 块上,您试图将数字的freq放入freq数组中。

But all you do is:但你要做的就是:

   int value = 0;
   .....
   score[row][col] = value;
   freq[value]++;`

firstly you are changing the score array,( which is a problem for you I guess...) and the you go to freq[0] and do ++ .首先,您正在更改score数组,(我猜这对您来说是个问题...)然后您转到freq[0]并执行++ Obviously modal is 0, that number appears in all of the array.显然 modal 是 0,这个数字出现在所有的数组中。

SOLUTION: in the first double for block you should do:解决方案:在第一个 double for 块中,您应该执行以下操作:

        value = score[row][col];
        freq[value]++;

so I think you mixed up the order of the line, it should be the other way around.所以我认为你混淆了行的顺序,应该是相反的。

private static void printMode(double[][] doubles) {
    HashMap<Double , Double> map = new HashMap();

    for (int i = 0; i < doubles.length; i++) {
        for (int j = 0; j < doubles[i].length; j++) {
            double temp = doubles[i][j];
            if(map.get(temp)==null){
                map.put(doubles[i][j],1.0);
            }else {
                double temp2 = (double) map.get(temp);
                map.put(doubles[i][j],++temp2);
            }
        }
    }
    Object[] objects = map.values().stream().sorted().toArray();
    Stream stream = map.entrySet().stream().filter(val-> val.getValue().equals(objects[objects.length-1]));
    stream.forEach(System.out::println);
}

I think using Stream for finding mode is the best way.我认为使用 Stream 查找模式是最好的方法。 use int instead of double doesn't cause any problems.使用 int 而不是 double 不会引起任何问题。

int value = 0;
int [] freq = new int [arr.length];
for (int i = 0; i < arr.length; i++){
   for (int j = 0; j < arr[i].length; j++){
     value = arr[i][j];
     freq[value]++;
   }
} 
int largest = 0;
int mode = 0;

for (int i = 0; i < freq.length; i++) {
    if (freq[i] > largest)
    {
        largest = freq[i];
        mode = i;
    }
}
System.out.println("modal score is: " +mode);

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

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