Histogram with different sized bins and edges to illustrate wave periods
2 views (last 30 days)
Show older comments
Henning Eimstad
on 9 Nov 2019
Commented: Henning Eimstad
on 12 Nov 2019
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.
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!
0 Comments
Accepted Answer
Ajay Pattassery
on 12 Nov 2019
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);
More Answers (0)
See Also
Categories
Find more on Histograms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!