Histogram with data frequency

23 次查看(过去 30 天)
Fadhil Ahmad Qamar
回答: dpb 2020-4-7
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

回答(2 个)

Jamal Nasir
Jamal Nasir 2020-4-7
%%%%%%% 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 2020-4-7
nc=accumarray(X.',Y.');
bar(1:numel(nc),nc)

类别

Help CenterFile Exchange 中查找有关 Histograms 的更多信息

标签

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by