How to plot histogram?

1 回表示 (過去 30 日間)
Uros Jankovic
Uros Jankovic 2018 年 10 月 24 日
回答済み: Image Analyst 2018 年 10 月 24 日
Hello,
I have a code for plotting the signal like this :
samples=20;
mean_value=-4.2;
std_dev=2.3;
t=0:1:samples-1;
s=mean_value+std_dev*randn(1,samples);
plot(t,s);
and i want to create histogram, but i have error saying:"attempted to access hist(0)" or hist(-1). I have a code like this and I don't know what is the problem:
%histogram
nbins=10;
xmin=-15;
xmax=5;
hist=zeros(1,nbins);
w=(xmax-xmin)/nbins;
x=xmin+w:samples/nbins:xmax+w;
for i=1:length(s)
if s(i)<= xmax && s(i)>= xmin
idx=floor(s(i)-xmin/w)+1;
hist(idx)=hist(idx)+1;
end
end
bar(x,hist/trapz(x,hist));
hold on
x2=xmin:0.1:xmax-0.1;
y=normpdf(x2,mean_v,std_dev);
plot(x2,y);
I am pretty new at this and could use some help. Thank you.

回答 (2 件)

Steven Lord
Steven Lord 2018 年 10 月 24 日
Why not just call the histogram function introduced in release R2014b?
  1 件のコメント
Uros Jankovic
Uros Jankovic 2018 年 10 月 24 日
I just wanted to understand how does it work this way, and I will also gain some knowledge that I might need in future.

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


Image Analyst
Image Analyst 2018 年 10 月 24 日
hist() is an old deprecated function. Don't use it for the name of your variable.
Also you must make sure that idx never is 0 or negative or a non-integer floating point number.

カテゴリ

Help Center および File ExchangeHistograms についてさらに検索

製品


リリース

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by