How can i populate an array using a for loop?

2 次查看(过去 30 天)
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
You have just to pre-allocate before the loop
A=zeros(1,n)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by