Storing values in a vector using a loop

4 次查看(过去 30 天)
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 个评论
Matt J
Matt J 2025-4-20
移动:Matt J 2025-4-20
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 2025-4-20
编辑:Annie 2025-4-20
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

请先登录,再进行评论。

回答(1 个)

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

类别

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