how to make new variables and values in for loop

5 ビュー (過去 30 日間)
Young Chan Jung
Young Chan Jung 2019 年 6 月 17 日
編集済み: madhan ravi 2019 年 6 月 17 日
I want to make and define variables with for loop like:
for i=1:3
something
Ai = ...
something
end
so that after running this, I have defined variables of Ai, or A1, A2, A3 in this case
I tried to search and found out there is something such as A{i} or saving values to arrays, but in the end I need to transform them again as the above and get separate defined variables...
Thank you.
  2 件のコメント
Stephen23
Stephen23 2019 年 6 月 17 日
編集済み: Stephen23 2019 年 6 月 17 日
"I have defined variables of Ai, or A1, A2, A3 in this case"
Do NOT do this. The MATLAB documentation specifically recommends against dynamically accessing variable names, as do all experienced MATLAB users. Read this to know why:
"I tried to search and found out there is something such as A{i} or saving values to arrays,"
This is exactly what you should do. Indexing is simple, neat, easy to debug, and very efficient (unlike what you are trying to do).
":..but in the end I need to transform them again as the above and get separate defined variables..."
I very much doubt that you "need" to do that. Please explain your usecase in more detail.
madhan ravi
madhan ravi 2019 年 6 月 17 日
編集済み: madhan ravi 2019 年 6 月 17 日
Adding to Stephen‘s comment , also learn to preallocate!

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

採用された回答

Star Strider
Star Strider 2019 年 6 月 17 日
Please do not create numbered variables!
If you want varying values for different iterations, subscript ‘A’ instead:
A(i) = ...
or:
A{i} = ...
depending upon what you want to do, and the size of ‘A’.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by