function hist3 number of bins with 'Edges' option doesn't count the bins right
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Johann Thurn
el 16 de Ag. de 2018
I am using hist3 to histogram 2D scattered data. I intended to use the 'Edges' option the same way as in histogram. But it doesn't do the same. I have defined an edges cell to histogram my data in 40x40 bins using something like {0:1/40:1 0:1/40:1}. This gives me 41 edges on each axes and thus should be 40x40 bins. But I get 41x41. If I use the same cell as centers with the 'Ctrs' option I also get 41x41, which is right. Something is fishy here. The 'Edges' option doesn't seem to work right. Is that a bug or am I doing something wrong?
The code looks something like that:
%define bin edges
%limits = [xmin xmax ymin ymax]
%bin = [binNumX binNumY]
binSizeX = (limits(2)-limits(1))/bins(1);
binSizeY = (limits(4)-limits(3))/bins(2);
edgsX = limits(1):binSizeX:limits(2);
edgsY = limits(3):binSizeY:limits(4);
edgs = {edgsX edgsY}; %what I wanted to use in hist3, but doesn't work as expected, 1 bin too much
%create histograms
%2D histogram
%define bin centers (edges option does some crap and they are needed anyway)
ctrsX = edgsX-binSizeX/2; ctrsX(1) = [];
ctrsY = edgsY-binSizeY/2; ctrsY(1) = [];
%add two bins to be removed afterwards (unwanted open bins)
ctrs = {[ctrsX(1)-binSizeX ctrsX ctrsX(length(ctrsX))+binSizeX] [ctrsY(1)-binSizeY ctrsY ctrsY(length(ctrsY))+binSizeY]};
%scattered data points = [Ax1 Ax2]
%Hist2Dbin = hist3([Ax1 Ax2],'Edges',edgs)'; %gives one bin too much
Hist2Dbin = hist3([Ax1 Ax2],'Ctrs',ctrs)';
%remove first and last bins (open bins, because of 'Ctrs' option?)
Hist2Dbin = Hist2Dbin(2:size(Hist2Dbin,1)-1,2:size(Hist2Dbin,2)-1);
Ax1bin = [ctrsX; sum(Hist2Dbin,1)]';
Ax2bin = [ctrsY' sum(Hist2Dbin,2)];
%1D histograms
Ax1binTotal = [ctrsX; histcounts(Ax1,edgsX)]';
Ax2binTotal = [ctrsY; histcounts(Ax2,edgsY)]';
8 comentarios
Adam Danz
el 16 de Ag. de 2018
I see. When I asked for a section of your code, I was assuming it included the lines that were problematic to you. I'll provide an answer below.
Respuesta aceptada
Adam Danz
el 16 de Ag. de 2018
Editada: Adam Danz
el 6 de Feb. de 2020
The reason why the 2nd output of hist3() provides 1 extra value is because it includes the last outer edge for any data that extends past the last bin. This is explained in the documentation .
Look at the values of your 'edges' and the values of the output bins.
edges = 0 : 0.025 : 1;
length(edges)
ans = 41
[N, c] = hist3(data, edges);
length(c)
ans = 41
c
ans = [0.0125 : 0.025 : 1.0125];
Notice that the last bin is greater than 1 which was your last edge.
Read more about this in the link I provided under 'edges'.
Más respuestas (1)
Steven Lord
el 16 de Ag. de 2018
Instead of using hist3 I recommend you use histogram2.
3 comentarios
Steven Lord
el 16 de Ag. de 2018
Ah, if you just want the binned data and not the figure then use histcounts2 instead.
Ver también
Categorías
Más información sobre Data Distribution Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!