Remembering Variables In for Loop
4 次查看(过去 30 天)
显示 更早的评论
I wrote the following code
no_of_values=input('How many Values of the independant valriable (x) are there');
for i=0:1:no_of_values-1
x_i=input(['Enter the value of x_ ',num2str(i)]);
y_i=input(['Enter the value of y_ ',num2str(i)]);
end
I want to save the value of x_i and y_i after every iteration, like x_0 and x_1 etc. But when the loop ends Matlab only remembers x_i and y_i as
variables for the last values.
The error can be seen in the screenshot attached
采纳的回答
Alan Stevens
2020-9-3
for i=1:no_of_values
x(i)=input(['Enter the value of x_ ',num2str(i)]);
y(i)=input(['Enter the value of y_ ',num2str(i)]);
end
Matlab starts counting at 1 rather than 0.
Store the values in x(i) and y(i).
更多回答(0 个)
另请参阅
类别
在 Help Center 和 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!