How can i populate an array using a for loop?

3 ビュー (過去 30 日間)
Nathan Poll
Nathan Poll 2014 年 12 月 5 日
回答済み: Azzi Abdelmalek 2014 年 12 月 6 日
I need to create a 1D array of n elements. The array needs to be populated with a different random number in each element using a for loop. So far, I have:
prompt = 'Enter a value for n:' ; n = input(prompt); for i = 1:n A(1,i) = rand end
It's not quite working right. Any tips?
  1 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 12 月 6 日
Why it's not working?

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

回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 12 月 6 日
You have just to pre-allocate before the loop
A=zeros(1,n)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by