Prev: imrotatemex
Next: naming a .NET component
From: Marios Karaoulis on 19 May 2010 14:59 I need some advice i have this loop and I want it to vectrorize it. sum=0; for i=1:8 for j=1:8 sum=sum+a(i)*b(j)*c(i,j); end end How can I do it
From: Walter Roberson on 19 May 2010 15:17 Marios Karaoulis wrote: > I need some advice > > i have this loop and I want it to vectrorize it. > > > sum=0; > for i=1:8 > for j=1:8 > sum=sum+a(i)*b(j)*c(i,j); > end > end > > How can I do it Start by pulling constant factors out. a(i) is independant of j, so if you calculate sum b(j)*c(i,j) over j and multiply that sum by a(i) you will have a partial speedup. After you have coded for that, look and see what else does not need to be calculated over and over again.
From: Matt Fig on 19 May 2010 15:31 Assuming a and b are 1-by-N, and c is N-by-N, S2 = (a'*b).*c; S2 = sum(S2(:)) Also, I wouldn't use the built-in function name 'sum' as a variable if I were you!
From: Bruno Luong on 19 May 2010 15:37 > How can I do it a(:).'*c*b(:) Bruno
From: Marios Karaoulis on 19 May 2010 15:42
Thank you all, I am trying in them and I will check. |