简体   繁体   English

C中的频率直方图

[英]Frequency Histogram in C

有人可以给我一个关于直方图伪代码是什么样子的提示吗?

How to structure and fill a histogram?如何构建和填充直方图?

Trivial case is just a count per bin:琐碎的情况只是每个 bin 的计数:

/* needs error checking, badly */
int *buildHist(int bins, double min, double max, int n, double *data){
   double *hist=malloc(bins*sizeof(int));
   if (hist == NULL) return hist;
   for (int i=0; i<n; ++i){
      int bin=int( (data[i]-min)/((max-min)/(bins)) );
      if ( (bin>=0) && (bin<n) ) hist[bin]++;
   }
   return hist;
}

For a weighted histogram, the array must be of floating point type.对于加权直方图,数组必须是浮点类型。

With more data (over- and under-flow counts, accumulated statistics...or even to keep the limits in the same place as the count), use a structure that includes the array.对于更多数据(上溢和下溢计数、累积统计信息……甚至将限制保持在与计数相同的位置),请使用包含数组的结构。

Incremental filling is often desired, but should be obvious from here.通常需要增量填充,但从这里应该很明显。

Output depends a great deal on what display technology you have at hand.输出在很大程度上取决于您手头的显示技术。

Well, you'd probably have a bunch of printf statements at the top for your headers to give some meaning to the data.好吧,您可能在标题顶部有一堆 printf 语句,以便为数据赋予一些含义。 Then maybe a line of dashes or equals or something to work as a separator.然后可能是一行破折号或等号或用作分隔符的东西。

Then below that, loop through an array with the values you wish to plot.然后在其下方,循环遍历一个包含您希望绘制的值的数组。 One on each line.每行一个。

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

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