2d histogram binning

3 views (last 30 days)
Adrian Szatmari
Adrian Szatmari on 9 Sep 2017
Commented: Cedric on 9 Sep 2017
Hi Folks!
I have written the following Matlab code that works. It takes two set of measurements of different dimension for the same points, and builds "rectangle" histograms from it. My goal is to get the array res, where res(i,j1,j2) = the number of measurements for point i in both bin j1 for the first histogram and bin j2 in the second histogram. Voyez plutot:
clear all
nb = 500;
dim1 = 50;
dim2 = 70;
measurement1 = rand(nb,dim1);
measurement2 = rand(nb,dim2);
edges1 = linspace(0,1, 10);
edges2 = linspace(0,1, 6);
res = zeros(nb, length(edges1), length(edges2));
for i = 1:nb
[N1 EDGES1 BIN1] = histcounts(measurement1(i,:), edges1);
[N2 EDGES2 BIN2] = histcounts(measurement2(i,:), edges2);
for j1 = 1:length(edges1)-1
for j2 = 1:length(edges2)-1
res(i,j1,j2) = sum((BIN1 == j1) & (BIN2 == j2));
end
end
end
It is totally unsexy, especially the triple for loop. Could anyone suggest a better way of doing this? Thanks in advance!
  1 Comment
Cedric
Cedric on 9 Sep 2017
It cannot work as written here, because BIN1 and BIN2 don't have the same dimension. And plutôt has a hat ;-)

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 9 Sep 2017
Why not use histogram2()?
  2 Comments
Adrian Szatmari
Adrian Szatmari on 9 Sep 2017
Edited: Adrian Szatmari on 9 Sep 2017
histogram2(), seems to do what I want, however I do not need the figure display. What I need is speed, since I am doing this thousands of times. I opened histogram2(), but did not really understand the underlying code.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!