How to work on multiple arrays with one for loop

25 ビュー (過去 30 日間)
진욱 강
진욱 강 2022 年 7 月 17 日
回答済み: M.B 2022 年 7 月 18 日
hello, lest say i have n number of arrays
a1=[1,2,3,...]
a2=[1,2,3,...]
a3=[1,2,3,...] and all the way to an=[1,2,3,...]
and i want to create a for loop that manipulates all the arrays.
could i use that fact that they are all named as "a" +"num" to accomplish this task?
for example,
for i=1:n
'a'+'i' = ... (i know this doesnt work. i just wanted to illustrate my point)
something like this above.
I dont wat to write down all the arrays in the loop to work on them is my point.
Thanks in advance!
  1 件のコメント
KSSV
KSSV 2022 年 7 月 17 日
Why did you save those many arrays like that? This is not suggested. While creating you have to create them into a matrix or cell array.

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

回答 (1 件)

M.B
M.B 2022 年 7 月 18 日
We need more information about how you define your variables and their size to provide you with the best solution.
In the meantime, try this:
a{1}=[1];
a{2}=[1,2];
a{3}=[1,2,3];
% ...
a{n}=[1,2,3,...,n];
for i=1:n
a{i} = do_something_to(a{i});
end

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by