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

1 件のコメント

Taha Nadeem
Taha Nadeem 2020 年 9 月 3 日
Thank You in Advance for Answering

サインインしてコメントする。

 採用された回答

Alan Stevens
Alan Stevens 2020 年 9 月 3 日

0 投票

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).

3 件のコメント

Taha Nadeem
Taha Nadeem 2020 年 9 月 3 日
Thanks. It worked
madhan ravi
madhan ravi 2020 年 9 月 3 日
Don’t forget to preallocate and use sprintf() instead.
Stephen23
Stephen23 2020 年 9 月 4 日
編集済み: Stephen23 2020 年 9 月 4 日
As madhan ravi wrote, preallocation would be a good idea (more efficient, avoid bugs):

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

リリース

R2020a

質問済み:

2020 年 9 月 3 日

編集済み:

2020 年 9 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by