Storing values in a vector using a loop

4 vues (au cours des 30 derniers jours)
Annie
Annie le 19 Avr 2025
This is a simple version of what i'm trying to implement in a function:
d = zeros(1,N)
for i = 1:N
d(1,i)=input('Distance = ');
end
I thought I was storing the ith value in the vector, but when it saved to my workspace it came out as a scalar!?
How do I create a vector of values using a loop? Why is it not working?
  4 commentaires
Matt J
Matt J le 20 Avr 2025
Déplacé(e) : Matt J le 20 Avr 2025
Note that there is no reason you need to do this with a loop. A vector can be entered with a single input statement, e.g.,
>> d=input("Distances = ")
Distances = [1,2,5,7]
d =
1 2 5 7
Annie
Annie le 20 Avr 2025
Modifié(e) : Annie le 20 Avr 2025
Yes, I'm aware. However, this is not the only case for which I'm using this method. Anyway, I realized my error and it's fixed.
Thank you though

Connectez-vous pour commenter.

Réponses (1)

Steven Lord
Steven Lord le 20 Avr 2025
Edit: Is there a way to run a function line by line like a live script?
Yes, the Debugger.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by