Swapping from hist to histogram
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
채택된 답변
Star Strider
2017년 2월 19일
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
Jason
2017년 2월 19일
Thankyou
Star Strider
2017년 2월 19일
My pleasure.
Jason
2017년 2월 20일
Is it too late to ask how to force the number of bins such that each bin is an integer i.e. 1,2,3 all the way upto the max value in my image?
Star Strider
2017년 2월 20일
It’s never too late, although I’m not at my computer 24x7 so delays are occasionally inevitable.
From the documentation:
- histogram(X,edges) sorts X into bins with the bin edges specified by the vector, edges. Each bin includes the left edge, but does not include the right edge, except for the last bin which includes both edges.
Specifying the bin edges (ranges) is certainly an option. You will have to experiment to get the result you want.
Jason
2017년 2월 20일
Thank you
Star Strider
2017년 2월 20일
My pleasure.
So I have tried to play with this. I have created my edges:
edges=[1:max(data)]
h=histogram(data,edges) ;
counts=h.Values;
bar(edges,counts,'g','EdgeColor','g');
but when I try to bar chart this,
as the length of edges is 1 more than the counts
Error using bar (line 143)
X must be same length as Y.
Look at the lengths of edges and counts. edges has one more element. So you need to decide if you want the left edge, right edge, or center of the bin. To get the left edges:
bar(edges(1:end-1), counts, 'g', 'EdgeColor', 'g');
Jason
2017년 2월 20일
Thanks
Steven Lord
2018년 11월 29일
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
2019년 1월 10일
thankyou for this.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
