Implement an arbitrary number N nested for loop

12 ビュー (過去 30 日間)
Chun Tat
Chun Tat 2022 年 12 月 8 日
コメント済み: Walter Roberson 2022 年 12 月 12 日
In my project, I'm looking for a way to create a nested for loop up to a fixed number N, for my purpose, the N is an integer smaller than 300, therefore it's useful to create such a function. To explain it better my problem, i would illustrate with an example:
list_to_loop = [1,a,b,c]
% Psuedo code:
for i in list_to_loop:
for j in list_to_loop:
for k in list_to_loop:
Dosomething()
end
end
end
I guess you already see the logic behind, in this example N = 3, i.e. I get a 3 layer nested for loop. I have seen answers to similar questions before but the answers were not general enough or its not very easy to understand because i come from a python background. Is there a very simple and elegant way to do this?
  10 件のコメント
Bruno Luong
Bruno Luong 2022 年 12 月 12 日
編集済み: Bruno Luong 2022 年 12 月 12 日
@Chun Tat ". And this is the solution I found, known as the "odometer" pattern"
I don't think you understood what we try to tell you.
"odometer" simply avoid to store all the combinations in the memory.
But it does speed up the calcultion and reduce the number of combination. You still have to wait unter universe reaches back-hole age to get your result.
Walter Roberson
Walter Roberson 2022 年 12 月 12 日
The odometer pattern is not, in itself, "smart". It just tries all possibilities in sequence, without having to store the values, but it does not (without additional logic) have the ability to do short-cuts rejecting possibilities. If for example it turned out that the second coefficient needed to be 1 less than the first coefficient, then it would not be able to detect that immediately after changing the second coefficient, and would instead run through all possibilities for the 3rd to 100th coefficient before incrementing the second coefficient.
The odometer pattern is pure "brute force". it does not even try to optimize the search -- not without additional logic.

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

採用された回答

Walter Roberson
Walter Roberson 2022 年 12 月 8 日
Is there a very simple and elegant way to do this?
No.
There are relatively compact methods involving ndgrid() and cell arrays, but for N = 100, you will run out of memory.
I have posted source code for generalized looping that has very low memory overhead (as long as you do not try to save all of the outputs); see https://www.mathworks.com/matlabcentral/answers/109622-how-can-i-write-n-for-loops-just-by-a-single-command#answer_118218 . In the comment there, the second version I link to is an iterator for the case where the different slots can have different numbers of entries and different data types.
  1 件のコメント
Chun Tat
Chun Tat 2022 年 12 月 12 日
@Walter Roberson Thank you Walter Roberson, after reading the answers "odometer" pattern you gave to similar questions, i managed to get mine working!

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

その他の回答 (1 件)

Bruno Luong
Bruno Luong 2022 年 12 月 8 日
Assuming a not so bug N of 100, and each of your list_to_loop has 2 elements, and you can compute 1000000000 (1e9) Dosomething() per second
You need
timeinyear = (2^100)/(1e9)/(3600*24*365)
timeinyear = 4.0197e+13
years to compute your nested loop. Still want to try?
  1 件のコメント
Chun Tat
Chun Tat 2022 年 12 月 12 日
編集済み: Chun Tat 2022 年 12 月 12 日
@Bruno Luong I'm fully aware of this problem but thank you for the comment.

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by