MATLAB: How to get rid of excess “for loop”

for looppossibility

Hi, I have a problem about possibility. I solve my problem for lower values of variables but I confused when I try solve it for higher variables. I want to select one value between 10 variables in 24 times. That means I have 10^24 mathematical possibility. First I try it for 3 variables and for 2 times. Here is my code
In my exact problem, I have 24 t values.(t1…t24). Also each t has got 10 same variables. (t=[1 2 3 4 5 6 7 8 9 10 ]). So I think that I have to use for loop in 24 times. And this will be too difficult for me. So is there any easy way to solve it?

Best Answer

Notice that in your code, result(k,1) is independent of the value of the "for j" loop, and result(k,2) is independent of the value of the "for i" loop. Your code can then be replaced by
for i = 1 : length(t1)
result1(i) = t1(i) * price(1);
end
for j = 1 : length(t2)
result2(j) = t2(j) * price(2);
end
which in turn can be shortened to
result1 = t1 * price(1);
result2 = t2 * price(2);
Notice that the space for this is linear, not exponential.
Where are the minimum values? Well
[min1, idx1] = min(result1);
but you don't even need to calculate that. Instead:
if price(1) == 0
min1 = 0; idx1 = 1; %everywhere will be 0
elseif price(1) > 0
[min1, idx1] = min(t1);
min1 = min1 * price(1);
else
[min1, idx1] = max(t1);
min1 = min1 * price(1);
end
This generalizes: the minimum of all possible products of lists of values occurs where each list is either minimum or maximum (which of those to choose will depend upon the signs of the terms. When you have a series of minimums and maximums of lists, you can incrementally find the minimum and maximum of their products, using only two storage locations; it does not even require 2^25 products be found.
If you have sums, then remember that the smallest sum occurs where each of the terms is smallest.
If you have an optimization problem, testing each possible combination of values is only efficient for a small number of values and variables. After that you should be using an optimization routine. For example fminmax() or perhaps ga()
My speculation at the moment is that you are attempting to solve an electrical load distribution problem.