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

In summary, you would need to convert the for loops into a function which takes in a matrix of indexes and calculates the best mixture as the one with the minimal negative difference.
  • #1
Jan Skacel
3
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
  • #2
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
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
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
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.
 

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

1. How can I use a for loop in MATLAB to iterate through an unknown number of variables?

One way to do this is by using the "eval" function to dynamically create and execute code. You can use a cell array or string array to store the variable names, and then use a for loop to iterate through the array and use "eval" to execute code for each variable.

2. Can I use multiple "for" loops in a single line of code?

Yes, you can use multiple "for" loops in a single line of code by separating them with a semicolon. For example: for i=1:10; for j=1:10; end; end;

3. Is there a way to break out of a "for" loop in MATLAB?

Yes, you can use the "break" statement to exit a "for" loop in MATLAB. This statement will immediately terminate the loop and continue with the rest of the code.

4. How can I use the "for" loop to iterate through a range of values?

You can use the colon operator (:) to specify a range of values to iterate through in a "for" loop. For example: for i=1:10 will iterate through the values 1, 2, 3, ..., 10.

5. Can I use a "for" loop to modify multiple variables at once?

Yes, you can use a "for" loop to modify multiple variables at once by using the "[]" indexing notation and specifying the variables you want to modify inside the brackets. For example: for i=1:10; [a(i), b(i)] = deal(i, i^2); end; will assign the values 1, 4, 9, ..., 100 to the variables a and b.

Similar threads

Replies
3
Views
2K
Replies
8
Views
2K
Replies
2
Views
1K
Replies
2
Views
3K
Replies
41
Views
9K
Replies
5
Views
1K
Back
Top