I have two n-by-m matrices, A
and B
. I want to create a new matrix C
which is something like:
for i = 1:n
C = C +
Have you profiled your for
loop code and found it to be too slow? If not, do it before you spend too much time agonizing over the loop penalty.
Your for
loop is not particularly bad because you loop only n
times but do O(n*m)
work each loop. Since you're doing a lot of work each iteration, the loop penalty doesn't hit as hard. The really bad situations are nested loops, e.g. if you calculated the outer products with nested for
loops too.