# How to plot the occurency of an element?

6 ビュー (過去 30 日間)
Stef 2018 年 7 月 24 日
コメント済み: Paolo 2018 年 7 月 24 日
I have a vector period, which considers 10 periods. I have another vector a, where some of the periods ocurr multiple times, others only one time and others do not appear. Therefore they do not have the same length. I want to plot the periods on the x-axis and the number of occurrences in a on the y-axis in a bar plot.
period = [1:10];
a = [1 1 1 2 3 3 6 6 6 6 6 7 8 8];
The bar at period 1 should be 3, at period 2 it should be 1, at period 3 it should be 2 and at period 4 it should be zero and so on as you can see from a

サインインしてコメントする。

### 採用された回答

Paolo 2018 年 7 月 24 日

For the vector a you specified period should be 1:8. Use:
period = [1:8];
a = [1 1 1 2 3 3 6 6 6 6 6 7 8 8];
n = histcounts(a);
bar(period,n)
or do you wish to have 9 and 10 in the graph too?

#### 4 件のコメント

Paolo 2018 年 7 月 24 日
Slightly hacky but you can do something like:
period = [1:8];
a = [1 1 1 2 3 3 6 6 6 6 6 7 8 8];
n = histcounts(a);
bar(period,n)
fig = gcf;
fig.Children.XAxis.TickValues = [1:10];
fig.Children.XLim = [0 10.5];
Guillaume 2018 年 7 月 24 日
Seems slightly more complicated than it needs to be:
a = [1 1 1 2 3 3 6 6 6 6 6 7 8 8];
histogram(a);
xlim([0.1 10.9]);
Paolo 2018 年 7 月 24 日
Uh good point Guillaume!

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!

Translated by