Histogram with different sized bins and edges to illustrate wave periods

조회 수: 2 (최근 30일)
I have a set of data (waveperiods) with corresponding number of observations as seen below.
waveperiods = [9 10 11 12 13 14 15 16 17 18 19 20 21]
observations = [1 5 14 22 28 5 6 2 1 0 1 0 0]
i.e a period of 9 is observed 1 time, and a period of 10 is observed 5 times etc....
I would like to make a histogram of the data, BUT with like a class system.
class 1 should contain all periods between 0 and 9.5
class 2 should contain all periods between 9.5 and 10.5 etc...
class 9 should contain all periods between 16.5 and 30.
Then the histogram should be nice and normal-ish like in the picture below.
histogram.jpg
However, I dont understand how to control the edges and make the desired groups...
I am stuck with the following code:
waveperiods = [9:21];
observations = [1 5 14 22 28 5 6 2 1 0 1 0 0];
groups = [0 9.5:1:16.5 30]; %Ive tried to make the intervals/groups....
Please help, thanks!

채택된 답변

Ajay Pattassery
Ajay Pattassery 2019년 11월 12일
The following code will plot the above bar graph
waveperiods = (9:21);
observations = [1 5 14 22 28 5 6 2 1 0 1 0 0];
groups = [0 9.5:1:16.5 30];
obsInRange = zeros(1,length(groups)-1);
rangeLength = length(groups)-1;
for i=1:rangeLength
waveperiodsIndex = waveperiods >= groups(i) & waveperiods < groups(i+1);
obsInRange(i) = sum(observations(waveperiodsIndex));
end
bar(1:rangeLength,obsInRange);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Histograms에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by