Histogram with data frequency

39 views (last 30 days)
Fadhil Ahmad Qamar
Fadhil Ahmad Qamar on 7 Apr 2020
Answered: dpb on 7 Apr 2020
I need to make a histogram that each of the data have their own frequency
say:
X=[ 4 3 1 12 15 3 8 15 13 20 4 12 15 16 16 2 2 15 18 4 ]
is the data and
Y=[ 2 1 1 1 2 2 1 1 1 1 1 2 2 2 1 2 1 2 1 2 ]
is the frequency of each of the data
and I need to bin these data into 20 bins, such as
binEdges = linspace(0, 1, 20+1)
a normal histogram of X would show the frequency of "4" as 3 because there are 3 of them in the X. What I want to achieve is to have a histogram that would show a frequency of 5 for "4" because there are two "4" with Y frequency of 2 and one "4" with Y frequency of 1. Same goes with the rest. How to code this logic?
Thanks,
Fadhil

Answers (2)

Jamal Nasir
Jamal Nasir on 7 Apr 2020
%%%%%%% you can use this code %%%%%%%%%%%
a=unique(x);
b=length(a);
for i=1:b
c(i)=sum(y(x==a(i)));
end
bar(c)
%%%%%%%%% jamal %%%%%%%%%%%

dpb
dpb on 7 Apr 2020
nc=accumarray(X.',Y.');
bar(1:numel(nc),nc)

Tags

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!