Storing values in a vector using a loop
显示 更早的评论
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 个评论
Torsten
2025-4-19
N = 3;
d = zeros(1,N)
for i = 1:N
d(1,i)=input('Distance = ');
end
d
This will prompt you three times to give a distance value - and after the loop, d will come out as 1x3 vector with elements being the input values you used.
回答(1 个)
Steven Lord
2025-4-20
1 个投票
Edit: Is there a way to run a function line by line like a live script?
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!