I've got a large (4000 values) set of unsorted, normally distributed points. I'm taking each of these data points into bins whose limits are in the BinLimit array. Then I'm tabulating the number of values in each bin.
X is the array of raw data, and N is the number of data points. The number of bins desired (TotalBins) is specified by the user.
Method #1
for i=1:TotalBins
Freq(i) = length(find(X >= BinLimit(j) & X <= BinLimit(j+1)));
j = j + 1;
end
Method #2:
sort(X);
for i=1:N
if (X(i) >= BinLimit(j) && X(i) <= BinLimit(j+1))
Freq(j) = freq(j) + 1;
elseif (j < TotalBins)
Freq(j+1) = freq(j+1) + 1;
j = j + 1;
end
end
Now, I know that the first solution is slower - for normal values of Total Bins (25-50) it's about 8 times slower but I'm wondering if there's a faster, more efficient solution than what I'm doing in Method #2.
Also, this is homework, but this is really just out of curiosity as either of the above solutions do the job - it's a chem class.