Histogram with counting number of "1" per bin

조회 수: 5 (최근 30일)
MiauMiau
MiauMiau 2015년 5월 4일
댓글: Image Analyst 2015년 5월 5일
Hi,
I have the following data: The intensity of a stimulus can be varied on a certain range, subjects have to say then for each intensity, if they can perceive the stimulus. Out of this data I would like to plot a histogram - and so far I have not found any existent Matlab function to do so (?).
So say the data would look like:
A = [100 120 80 85 130 145 160 95 105 110 115 135 150;
0 1 0 0 1 1 1 1 1 1 0 0 1]
and I would like to split up the range [80,160] into 4 bins and count the number of 1's for each bin.

채택된 답변

Image Analyst
Image Analyst 2015년 5월 4일
Try this:
A = [100 120 80 85 130 145 160 95 105 110 115 135 150];
counts = [ 0 1 0 0 1 1 1 1 1 1 0 0 1]
edges = linspace(80, 160, 5) % % 5 edges for 4 bins
myHistCounts = histcounts(A.*counts, edges)
works as long as your counts are only 0 and 1. You can have two of the same value, like 2 at 120 with a 1 as long as the 120's are separate, which I think they would be because each element is for one stimulus.
  댓글 수: 3
MiauMiau
MiauMiau 2015년 5월 5일
And how do I plot that afterwards?
Image Analyst
Image Analyst 2015년 5월 5일
You can use plot() or bar(). I tend to prefer bar() for histograms.
bar(edges(1:end-1), myHistCounts, 'BarWidth', 1, 'FaceColor', 'b');
or something like that

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Ahmet Cecen
Ahmet Cecen 2015년 5월 4일
A trick for that would be to covert that data into this:
B=[120, 130, 145, 160, 95, 105, 110, 150];
basically keeping the only the values that has 1s below. Now you can use the hist function properly to get a histogram easily.
You can use the binranges argument to specify those bins from 80 to 160 by 20. Check:
doc hist

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by