我有一个代码如下:
Ne = 100;
H = rand(Ne,Ne);
g = zeros(Ne,1);
for e =1:Ne
hue = H(:,e);
ss1 = bsxfun(@times, hue', hue) .* M; % M is a Ne*Ne matrix
g(e) = sum(ss1(:));
end
当 Ne > 1000 时,它运行非常缓慢。
我阅读了matlab文档,发现permute函数是一种可能的加速方法。但是我试了一整天,失败了。
这是我的代码,我不知道出了什么问题。
C = permute(bsxfun(@times, permute(H, [1 3 2]), permute(H', [1 3 2])), [1 3 2]);
g = sum(sum(C))