How can i populate an array using a for loop?
Afficher commentaires plus anciens
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 commentaire
Azzi Abdelmalek
le 6 Déc 2014
Why it's not working?
Réponses (1)
Azzi Abdelmalek
le 6 Déc 2014
You have just to pre-allocate before the loop
A=zeros(1,n)
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!