# [matlab] How to program unknown number of 'for' loops with i1, i2,iN variables? Or do it smart way

Tags:
1. Oct 16, 2014

### Jan Skacel

Hi :)
I need to program several nested 'for' loops which differs only in index of their variables inside.
So I need to convert this:

for i1 = 1000:-d:0
for i2 = (1000-(d*i1):-d:0
....
for in = (1000-(d*in-1):-d:0

some function containing all index variables like:
b=(i1*A( :,2), i2*A( :,3), ... in*A( :,n))
c=b/a
d=min(c)
if d<e
e=d
end
end​
end​
end​

into some smart one 'for-loop'. The number of for loops is determined by number of vectors, or rather number of columns in matrix "A" specified prior to the loop.
I'm guessing there should be some array trick instead of indexes i1, i2, in. Something like for A(1,: ) :1000:-d:0.
Or should I somehow call function each time?

Thank you so much

2. Oct 17, 2014

### Staff: Mentor

Could you explain what it is supposed to do, write some mathematical equations?

For now, the only thing I can think of is that you will need some kind of recursion to calculate a vector of i's and use that to calculate b.

3. Oct 17, 2014

### Jan Skacel

So the purpose would be for situations when you have several mixtures, each with different percentage of ingredients and you are trying to find out one mixture (or rather mixture of mixtures) closest to target value of ingredients in it.

Example would be meals - you have several 'mixtures' (ham, bread, cheese, egg, avocado, pasta, honey.. ) and you are trying to find out how much of each to take if you are looking precisely for 300g proteins, 200g fat, 500g carbs,.... 500mg vit C, 80g fiber.. in 1000g mixture. In this weird example not caring that the result might be eeg+honey :D and calculating the best mixture as the one, which has the minimal negative difference compared to target. Meaning 300% of some ingredient doesn't matter, 30% does.
As I have taken the approach of calculating each possibility (1000g of A1, 0g A2, 0g A3; 999g A1, 1g A2, 0g A3; 999g A1, 0g A2, 1g A3, 998... ) the number of calculations will go really high the more mixtures and ingredients we are taking into consideration. So I'm trying to save into one (same) variable only the results which were better than previous best result. That is the 'if statement' inside the example of the 'for loops' i have posted. Storing into the variable vector of i1, i2, .. in which is also the amount of mixtures you have used for final 'best-so-far' mixture. Like e=(187 131 12 320 ..) meaning 187g ham, 131g eggs, 12 awesome sauce, 320g of pure happiness,...

4. Oct 17, 2014

### Staff: Mentor

Lets see. Imagine you take 7 ingredients, like in your first example, and you take 1000 possibilities for the mass of each ingredient, you then have $1000^7 = 10^{21}$ mixtures to compare. Assuming, for the sake of the argument, that each mixture could be evaluated in one clock cycle of your computer, and that your computer runs at 4 GHz, that would mean the time required to compare all the mixtures would be $10^{21} / (4 \times 10^9\ \mathrm{s}^{-1}) \approx 2 \times 10^{11}\ \mathrm{s}$, or about 6300 years! Even if you use smaller numbers, there is no way you can sample the entire parameter space in any reasonable time.

What you have is an optimization problem, and you need to use the tools that were developed for such tasks, for instance genetic algorithms.

5. Oct 17, 2014

### Jan Skacel

Oh.
That's a bummer. But now it seems so obvious. Ok then.
I would still like to know how to write those loops, just for the sake of learning matlab and well.. feeling like I have the answer for question that has been bothering me for hours.
But mainly I have to dig into the theory of those genetic algorithms. Looks much more complicated and I'll have to put it on back burner, but at least I know which way to go. Thank you very much, doctor.