matlab中的三重求和

triple summation in matlab

我想为下面的三重求和写一个matlab代码。假设 R=[r_{ij}] is a 6 by 6 矩阵给出如下:

 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<j<h 编写 \sum_{i=1}^{i=6} \sum_{j=1}^{j=6}\sum_{h=1}^{h=6}(r_{ih}+r_{hj}-r_{ij}-0.5)^2) 的代码。我按如下方式尝试了我的 matlab 代码,但我无法得到确切的答案 (0.6300)。请任何帮助?提前致谢!

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

你确定结果应该是 0.63???

根据你的描述,我觉得你的嵌套for循环可以这样写。

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

这给出了

p1 =  1.0335