MATLAB MATLAB: Program Unknown # of 'for' Loops w/ i1,i2,iN Variables Smartly

AI Thread Summary
The discussion revolves around optimizing the programming of multiple nested 'for' loops in MATLAB, where the number of loops corresponds to the number of columns in a matrix. The user seeks a more efficient approach to calculate mixtures of ingredients to meet specific nutritional targets, highlighting the impracticality of evaluating all possible combinations due to exponential growth in calculations. It is suggested that recursion or array manipulation could simplify the loop structure, but ultimately, the problem is identified as an optimization challenge better suited for genetic algorithms. The user expresses a desire to learn how to implement the loops in MATLAB while acknowledging the need to explore genetic algorithms for more efficient solutions. The conversation emphasizes the importance of understanding optimization techniques in complex computational problems.
Jan Skacel
Messages
3
Reaction score
0
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
 
Physics news on Phys.org
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.
 
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,...
 
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.
 
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.
 

Similar threads

Replies
2
Views
2K
Replies
8
Views
2K
Replies
5
Views
1K
Replies
4
Views
1K
Replies
1
Views
3K
Back
Top