Vectorize the code of a summation written with nested for loops
Mostrar comentarios más antiguos
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 comentario
Jonas Kreich
el 14 de Nov. de 2016
Respuesta aceptada
Más respuestas (1)
Jonas Kreich
el 14 de Nov. de 2016
Categorías
Más información sobre Vector Volume Data en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
