MATLAB: Calculation of a double sum

double sum for loop

Hey all!
i want to calculate the double sum over w_ij * v_((2+k*3)+i,(2+l*3)+j). The indices for i should run from -1 to 1 (so -1,0,1), same for j. The indices for k are from 0:893 and for l from 0:356. And w should be -8 if i=j=0, otherwhise it is w=1. i Tried:
for i = -1:1
for j = -1:1
if i == 0 & j==0
w = -8
else
w = 1
end
for k = 0:893
for l = 0:356
sum(w*v((2+k*3)+i,(2+l*3))+j)
end
end
end
end
% code
end
But the results were not as expected. Can you help me? Thanks!

Best Answer

mySum=0;
for i = -1:1
for j = -1:1
if i == 0 & j==0
w = -8;
else
w = 1;
end
for k = 0:893
for l = 0:356
mySum=mySum+( w*v((2+k*3)+i,(2+l*3))+j );
end
end
end
end
disp(['mySum = ' num2str(mySum)]);
Related Question