0
votes

I have my data in 2 vectors. Then using [n,xout] = hist(x,y) returns vectors n and xout containing the frequency counts and the bin locations. x are my real data and y the segment where I want to build my histogram. Than I use bar(xout,n) to plot the histogram. In the end I am fitting this histogram with gaussian fit.

Now I would like to know where my real data (each point of vector x), are located in the histogram? Can someone help me to figure out them?

2

2 Answers

1
votes
[~, result] =  min(abs(bsxfun(@minus, y(:), x(:).')));

This gives, for each value in x, the index of the closest element in y. So y(result) is the closest element in y for each x.

Example:

>> x = [0.4 1.6 5.3 4.2 3.1 7.8];
>> y = [0 2 4 6 8];
>> [~, result] =  min(abs(bsxfun(@minus, y(:), x(:).')))
result =
     1     2     4     3     3     5
>> y(result)
ans =
     0     2     6     4     4     8
1
votes

You want to use histc.

[binCounts, idx] = histc(x, y);

Then to find the bin in which a certain value of x is:

bin = idx(x == 0.4);

Just watch out since histc second input is not the centers like hist, but the end value of each bin. So you might need to change your y vector.