2d histogram binning
1 view (last 30 days)
Show older comments
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
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 ;-)
Answers (1)
See Also
Categories
Find more on Histograms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!