How can i populate an array using a for loop?

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일

0 개 추천

You have just to pre-allocate before the loop
A=zeros(1,n)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2014년 12월 5일

답변:

2014년 12월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by