【问题标题】:finding mode from histogram从直方图中寻找模式
【发布时间】:2013-04-16 15:30:16
【问题描述】:

我有这个函数来打印直方图,我想也可以使用这个函数计算模式,我知道要找到模式你需要比较每个分数的出现次数,但我不知道如何实现这进入代码。有没有办法实现这个功能来查找模式?

这是我的功能

int calMode(RECORD list[], int count){
int tempMode = 0;
int i, k;
int current = 0;

while (current < count)
{
    printf("%d:", list[current].score);
    for(k=0; (i=current + k) < count ; ++k)
   {
        if(list[current].score == list[i].score)
            printf("*");
        else
            break;
    }
      if(k > tempMode)
        tempMode = k;
    printf("\n");
    current = current + k;
}
printf("%d\n", tempMode);
   return tempMode;
}

【问题讨论】:

  • 如果你只是找到k的最大值,我想将它与你保存为最大值的值进行比较。但是,如果最大值相同。
  • "有没有办法实现这个功能来查找模式?" 是的。您还有其他问题吗?

标签: c structure histogram mode


【解决方案1】:
int calMode(RECORD list[], int count){
    int tempMode;
    int i, k;
    int current = 0;
    int max = -1;
    int update = 0;

    while (current < count){
        printf("%d:", list[current].score);
        for(k=0; (i=current + k) < count ; ++k){
            if(list[current].score == list[i].score)
                printf("*");
            else
                break;
        }
        printf("\n");

        if(k>max){
            max = k;
            tempMode = list[current].score;
            update  = 1;
        } else if(k == max){
            update = 0;
        }

        current = current + k;
    }
    if(update == 0)
        tempMode = -1;//indeterminable
    return tempMode;
}

【讨论】:

    【解决方案2】:

    嗯,你需要一个不同的算法。您需要找到您的项目的最大.score 成员,并存储相应的索引。这样的事情可能会做:

    int max = list[0].score;
    RECORD *max_item[count] = { &list[0] };
    size_t index = 1;
    while (count-- > 1) {
        // We're looking for any items that are greater than or equal to the
        // current max, so when we find items that are less, we jump back to
        // the condition evaluation using "continue".
        if (list[count].score < max) { continue; }
    
        // When we find a value that's greater than the current maximum, we
        // need to discard all previously stored "maximum" items and update
        // our current maximum.
        if (list[count].score > max) {
            index = 0;
            max = list[count].score;
        }
    
        // At this point, the current item can't be less than the current max.
        // This is because of the "continue;" earlier. Add this item to our
        // list of "maximum" items.
        max_item[index++] = &list[count];
    }
    

    max 现在将最高分数存储在直方图中,index 存储包含该最高分数的项目数,max_item 存储指向包含该项目的RECORDs 的指针。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-11
      • 2017-08-17
      • 2022-06-16
      • 2019-06-26
      • 2014-12-29
      • 1970-01-01
      • 1970-01-01
      • 2021-11-11
      相关资源
      最近更新 更多