フィルターのクリア

Histogram Not Plotting Correctly

10 ビュー (過去 30 日間)
Deborah Brooks
Deborah Brooks 2022 年 5 月 18 日
コメント済み: Steven Lord 2022 年 5 月 18 日
Hi, I'm trying to create a histogram for 100 random integers. Only problem is that the plot only shows one value - the last number. My 'for loop' is creating 100 integers, it's just not plotting. Thank you in advance, I'm trying to learn MATLAB by myself for next semester so sorry for asking here.
n = 100;
for k = 1:n
x = randi(5)
end
hist(x,10)

回答 (1 件)

KSSV
KSSV 2022 年 5 月 18 日
編集済み: KSSV 2022 年 5 月 18 日
n = 100;
x = zeros(1,n) ;
for k = 1:n
x(k) = randi(5) ;
end
hist(x,10)
Loop is not required.
n = 100;
x = randi(5,1,n) ;
histogram(x,10)
  1 件のコメント
Steven Lord
Steven Lord 2022 年 5 月 18 日
The histogram edges would look nicer (centered on the integer values) if you told the function the values you're binning are integers so it can set the bin edges accordingly.
n = 100;
x = randi(5,1,n) ;
histogram(x, 10, 'BinMethod', 'integers')

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by