Different number of for loops
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I want my input to be a number at least 9 or 10, say n, and I need a code to create n number of for loops. I need to change the number of loops, because I have many cases with different number of n variables.
Each loop starts from 0 but ends to a different number. In order to keep a solution, I have an if statement.
For example, for n=4 loops,
n = 4;
for x1 = 0:9
for x2 = 0:5
for x3 = 0:5
for x4 = 0:5
if x1+x2+x3+x4==10
....
end
end
end
end
end
For n=10 loops,
n = 10;
for x1 = 0:9
for x2 = 0:5
for x3 = 0:5
for x4 = 0:5
for x5 = 0:5
for x6 = 0:5
for x7 = 0:4
for x8 = 0:4
for x9 = 0:5
for x10 = 0:9
if x1+x2+x3+x4+x5+x6+x7+x8+x9+x10==32
.....
end
end
end
end
end
end
end
end
end
end
end
How I can create this code, using an index vector and one loop, or recursive function?
I have read:
- https://www.mathworks.com/matlabcentral/answers/345551-function-with-varying-number-of-for-loops
- https://www.mathworks.com/matlabcentral/answers/333926-recursive-function-for-replacing-multiple-for-loops
but I did not achieve my goal.
Thank you in advance!
0 Kommentare
Antworten (2)
KSSV
am 30 Sep. 2020
x1 = 0:9 ;
x2 = 0:5 ;
x3 = 0:5 ;
x4 = 0:5 ;
[x1,x2,x3,x4] = ndgrid(x1,x2,x3,x4) ;
thesum = x1+x2+x3+x4 ;
idx = thesum == 10 ;
iwant = [x1(idx) x2(idx) x3(idx) x4(idx)] ;
Now iwant has the values which obeys your condition. You can use them.
Wont this logic work?
8 Kommentare
KSSV
am 30 Sep. 2020
I tried for n = 4...ndgrid taking less time.
Show us the code whcih you have tried.
Walter Roberson
am 30 Sep. 2020
See https://www.mathworks.com/matlabcentral/answers/357969-using-recursive-function-to-calculate-all-possible-peptide-combinations#answer_282766 for what I refer to as the "odometer" pattern (thanks to Chris Torek for the naming suggestion.)
I show there how to use a single for loop to implement incrementing through any number of levels where each level has a finite list of permitted values. The general pattern does not require that the different levels are the same datatype (it is just that the code can be made more compact and use less memory if they are all the same datatype.)
This will not be faster than nested for loops.
There is no general pattern that is faster than nested for loops, because in general it is not always possible to vectorize the work to be done for each combination.
The example you post shows a test for a fixed sum. If the fixed sum is the only acceptable case, then starting around x6 instead of
for x6 = 0:5
for x7 = 0:4
you could
left5 = 32 - (x1+x2+x3+x4+x5);
for x6 = 0: min(5, left5);
left6 = left5 - x6;
for x7 = 0:min(4, left6)
That is, you already have enough information to be able to prune some of the possibilities.
But also consider that asking for the sum of a list of non-negative integer numbers to add up to a particular number, is called a "partition problem", and there are techniques for generating the list of matching values. See for example https://www.mathworks.com/matlabcentral/fileexchange/12009-partitions-of-an-integer
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!