开发者

Matlab code optimization and removing loops [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 11 years ago.

I can't remove this loops by myself, if anybody can help me with optimization this piece of code - welcome.

M = length(w);
expt = exp(-t .* (phis * w'));
G = zeros(M, M);
for i = 1 : M
    for j = 1 : M
        last = 2 开发者_如何转开发* reg_coef * eye(M);
        G(i,j) = last(i, j) + mean(expt .* t .^2 .* phis(:,i) .* phis(:,j) ./ (1 + expt) .^ 2);
    end
end
  • w - size is (1xM)
  • phis - size is (NxM)
  • t - size is (Nx1)


You can do alot better by just writing cleaner code - ensure that you allocate properly, ensure that you remove duplicate calculations from loops etc:

EDIT: You can also see that the resulting matrix G is symmetric, so you get an immediate 2x speed-up by only calculating the upper triangle and filling in the lower triangle afterwards as a transpose.

At least with my MATLAB another big speed-up is achieved by vectorising the call to mean() through the use of a temporary array.

N = 100;
M = 100;

w = rand(1,M);
t = rand(N,1);
phis = rand(N,M);
reg_coeff = rand(1,1);

expt = exp(-t .* (phis * w'));

%% Initial version
tic
for i = 1 : M
   for j = 1 : M
      last = 2 * reg_coeff * eye(M,M);
      G1(i,j) = last(i,j) + mean(expt .* t .^ 2 .* phis(:,i) .* phis(:,j) ./ (1 + expt) .^ 2);
   end
end
t1 = toc;

%% Faster version
tic
coeff = expt .* t .^ 2 ./ (1 + expt) .^ 2;
G2 = zeros(M,M);
TT = zeros(M,M);
for i = 1 : M
   for j = i : M % only form upper triangle
      TT(:,j) = coeff .* phis(:,i) .* phis(:,j);
   end
   G2(i,i:M) = mean(TT(:,i:M),1); % vectorise call to mean()
end
G2 = 2 * reg_coeff * eye(M,M) + G2 + triu(G2,+1)';
t2 = toc;

%% Compare versions
speed = t1/t2
error = max(max(abs(G2 - G1)))

For this 100x100 case the speed-up was around 41.0 on my machine.

Hope this helps.


You can, at least, take a lot of terms out of the loops:

last = 2 * reg_coef * eye(M);
A = expt .* t .^2;
B = (1 + expt) .^ 2;

for i = 1 : M
    X = A .* phis(:,i) ./ B;
    for j = 1 : M
        G(i,j) = mean(X .* phis(:,j));
    end
end

G = G + last;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜