Swapping from hist to histogram

9 visualizaciones (últimos 30 días)
Jason
Jason el 19 de Feb. de 2017
Comentada: Jason el 10 de En. de 2019
Hi, under the recommendation of Steven Lord, I am trying to update my code and not use hist as the histogram function and instead use histogram
I previously did this:
[counts,xb]=hist(data(:,3),nbins); %IMHIST ONLY HANDLES 8 & 16 BIT IMAGES, NOT 12BIT
figure
ax1=subplot(1,2,1);
bar(xb,counts,'b','EdgeColor','b');
grid on
%Now get the max frequency.
mxC=max(counts);
indx=find(counts==mxC);
xmx=xb(indx);
hold on;
lineH1=plot([xmx xmx],ylim,'r--','LineWidth',1);
hold off
So when I swap over to the histogram function, I can get the y-axis values but not the x-axis values
h=histogram(data(:,3),nbins)
counts=h.Values % frequency (i.e. y-axis)
How do I get the mode of this histogram and plot it?
Thanks Jason

Respuesta aceptada

Star Strider
Star Strider el 19 de Feb. de 2017
One approach:
data = randi(99, 1, 100);
nbins = 25;
h = histogram(data, nbins);
counts = h.BinCounts;
edges = h.BinEdges;
width = h.BinWidth;
ctrs = edges(1:end-1) + width/2;
MaxCountsV = counts >= max(counts); % Logical Vector
Desired_Output = [ctrs(MaxCountsV), counts(MaxCountsV)] % [BinCentre, Counts]
It’s essentially self-documenting with the variable names. The output displays the bin centre corresponding to the maximum count.
You could make this a bit more efficient if you need to. My objective here is to leave a clear trail so you know how I got the result.
  11 comentarios
Steven Lord
Steven Lord el 29 de Nov. de 2018
I happened upon this message while doing searching for another message. About the follow-up question about making each integer a different bin, when you call histogram specify the name-value pair 'BinMethod', 'integers' instead of specifying bin edges and histogram will automatically create edges with one integer per bin (unless your data spans too wide a range, in which case the bins will be wider than 1, as stated in the entry for 'integers' in the documentation of the BinMethod argument.)
Jason
Jason el 10 de En. de 2019
thankyou for this.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Histograms en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by