I would like to write a matlab code for the following triple summation. Assume that R=[r_{ij}] is a 6 by 6
matrix which is given as follows:
R=[.5 .5 .5 .8155 .5 .3423;...
.5 .5 .6577 .8155 .5 .3423;...
.5 .3423 .5 .88662 .75 .3423;...
.1845 .8145 .1338 .5 .25 .25;...
.5 .5 .25 .75 .5 .25;...
.6577 .6577 .6577 .75 .75 .5]
I want to write a code for \sum_{i=1}^{i=6} \sum_{j=1}^{j=6}\sum_{h=1}^{h=6}(r_{ih}+r_{hj}-r_{ij}-0.5)^2)
for i<j<h
. I tried my matlab code as follows, but I couldn't get the exact answer (0.6300). Please any help? Thanks in Advance!
p1=0;
for i=1:length(R)
for j=1:length(R)
for h=1:length(R)
if i<j && j<h
p1=p1+(R(i,h)+R(h,j)-R(i,j)-0.5)^2;
end
end
end
end
p1
Are you sure the result should be 0.63
???
According to your description, I think your nested for loop can be written in the following manner.
for i=1:(length(R)-2)
for j=(i+1):(length(R)-1)
for h= (j+1):length(R)
p1=p1+(R(i,h)+R(h,j)-R(i,j)-0.5)^2;
end
end
end
which gives
p1 = 1.0335