Hi,

I have a prog that takes in a 2D matrix "mat", of length "cols", and depth "rows" and whose max value is mx and min value is mn, and I want to do a simple histogram of 20 "buckets". I scribbled up some code, but I'm wondering if I can't make it more efficient. It seems a little laborious to me, somehow.

Thought I'd pop it up here to see if aybody's got any ideas. (BTW, I generally expect to get a bottom heavy histogram).

Code:

int *histo(double *mat, int cols, int rows, int nbu, double mx, double mn)
{
int i, j, k, *h;
double dh;
dh = (mx - mn)/ nbu; // size of each bucket / interval
h=calloc(nbu,sizeof(int));
for(i = 0; i < rows; i++)
for(j = 0; j < cols; j++) {
for(k=0;k<nbu-1;++k) {
if(mat[i*cols+j]<(k+1)*dh+mn) {
h[k]++;
break;
}
}
if(mat[i*cols+j]>=((nbu-1)*dh+mn))
h[nbu-1]++;
}
return h;
}