Take the 2-minute tour ×

I use 2D dataset like below,

37.0235000000000    18.4548000000000
28.4454000000000    15.7814000000000
34.6958000000000    20.9239000000000
26.0374000000000    17.1070000000000
27.1619000000000    17.6757000000000
28.4101000000000    15.9183000000000
33.7340000000000    17.1615000000000
34.7948000000000    18.2695000000000
34.5622000000000    19.3793000000000
36.2884000000000    18.4551000000000
26.1695000000000    16.8195000000000
26.2090000000000    14.2081000000000
26.0264000000000    21.8923000000000
35.8194000000000    18.4811000000000

to create a 3D histogram.

enter image description here

How can I find the histogram value of a point on a grid? For example, if [34.7948000000000 18.2695000000000] point is given, I would like to find the corresponding value of a histogram for a given point on the grid.

share|improve this question

3 Answers 3

up vote 2 down vote accepted

I used this code

point = feat_vec(i,:); // take the point given by the data set
X = centers{1}(1,:); // take center of the bins at one dimension
Y = centers{2}(1,:); // take center of the bins at other dim.  

distanceX = abs(X-point(1)); // find distance to all bin centers at one dimension 
distanceY = abs(Y-point(2)); // find distance to center points of other dimension

[~,indexX] = min(distanceX); // find the index of minimum distant center point
[~,indexY] = min(distanceY); // find the index of minimum distant center point for other dimension
share|improve this answer

You could use interp2 to accomplish that!

If X (1-D Vector, length N) and Y (1-D vector, length M) determine discrete coordinate on the axes where your histogram has defined values Z (matrix, size M x N). Getting value for one particular point with coordinates (XI, YI) could be done with:

% generate grid
[XM, YM] = meshgrid(X, Y);
% interpolate desired value
ZI = interp2(XM, YM, Z, XI, YI, 'spline')

In general, this kind of problem is interpolation problem. If you would want to get values for multiple points, you would have to generate grid for them in similar fashion done in code above. You could also use another interpolating method, for example linear (refer to linked documentation!)

share|improve this answer

I think you mean this:

[N,C] = hist3(X,...) returns the positions of the bin centers in a 1-by-2 cell array of numeric vectors, and does not plot the histogram.

That being said, if you have a 2D point x=[x1, x2], you are only to look up the closest points in C, and take the corresponding value in N.

In Matlab code:

[N, C] = hist3(data); % with your data format...
[~,indX] = min(abs(C{1}-x(1)));
[~,indY] = min(abs(C{2}-x(2)));
result = N(indX,indY);

done. (You can make it into your own function say result = hist_val(data, x).)


I just saw, that my answer in essence is just a more detailed version of @Erogol's answer.

share|improve this answer

Your Answer


