Vectorize the code of a summation written with nested for loops
Show older comments
Hi everyone,
I want to implement the following inequality in an efficient form (it's part of an optimization problem I'm solving with the genetic algorithm from the Global Optimization Toolbox):

I still realised it straight with some for loops, like you can see in the code:
for k = 1:K
summation = 0;
for w = 1:W
for m = 1:M
summation = summation + a(m) * y(m,k,w);
end
end
ineq(k) = summation - c(k);
end
And because Matlab is slow with loops, I want to vectorize my code. This is how far i got:
for w = 1:W
summation(w,k) = a(m) * y(m,k,w);
end
summation = sum(summation);
ineq = summation - c;
Finally I really don't how to replace the last for loop. It would be great if someone has a solution for it or another idea how to implement the inequality in vectorized form.
1 Comment
Jonas Kreich
on 14 Nov 2016
Accepted Answer
More Answers (1)
Jonas Kreich
on 14 Nov 2016
Categories
Find more on Performance and Memory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!