need help optimizing this code...

2 ビュー (過去 30 日間)
Walter
Walter 2012 年 3 月 27 日
how can I do this without a loop:
(consider vectors w and s of length n)...
dTot = 0;
for i=1:n
for j=i+1:n
v = w(i) * w(j) * (s(i)) * (s(j));
dTot = dTot + v;
end
end
dTot = dTot * 2;

採用された回答

Andrei Bobrov
Andrei Bobrov 2012 年 3 月 28 日
Variant
ws = w.*s;
dTot = bsxfun(@times,ws,triu(ones(numel(w),1)*ws.',1));
dTot = sum(dTot(:))*2;

その他の回答 (4 件)

Teja Muppirala
Teja Muppirala 2012 年 3 月 28 日
You can simplify the loop like this:
for i=1:n
v = w(i)*s(i)*sum( w(i+1:n) .* s(i+1:n) );
dTot = dTot + v;
end
dTot = dTot * 2
[EDITED:]
Aha! I knew there had to be an easier way to do this!
ws = w.*s;
Cws = cumsum(ws(n:-1:2));
dTot = 2 * (sum(ws(n-1:-1:1) .* Cws))
  3 件のコメント
Teja Muppirala
Teja Muppirala 2012 年 3 月 28 日
Is X + X is faster than 2*X? I'm not really sure... but just trying it out, it seems they are both about the same to me.
Andrei Bobrov
Andrei Bobrov 2012 年 3 月 28 日
+1

サインインしてコメントする。


Jakob Sørensen
Jakob Sørensen 2012 年 3 月 27 日
Wouldn't it work with:
% Turn vectors to matrix form
W1 = repmat(w, [n 1]);
W2 = repmat(w',[1 n]);
% Multiply these
Wm = W1 .* W2;
% Turn vectors to matrix form
S1 = repmat(s, [n 1]);
S2 = repmat(s',[1 n]);
% Multiply these
Sm = S1 .* S2;
% Multiply Wm and Sm matrices
Vm = Wm .* Sm
% Sum up and multiply by 2
V = 2*sum(Vm(:))
Note that I haven't tested this, but i think it should work...
  1 件のコメント
Jakob Sørensen
Jakob Sørensen 2012 年 3 月 27 日
Kinda missed the j = i+1 part, this changes quite a few things. But you might be able to modify the concept. Sorry about that...

サインインしてコメントする。


Kevin Holst
Kevin Holst 2012 年 3 月 27 日
I don't think that will work actually. Try this, it's a little convoluted, but I think it's correct:
% this assumes w and s are row vectors
wm = triu(repmat(w,[n 1]),1);
sm = triu(repmat(s,[n 1]),1);
wsm = wm .* sm;
wsv = w .* s;
dTot = 2*(sum(wsv*wsm));
If you're wanting a one liner that no one will be able to read or understand then:
dTot = 2*(sum((w.*s)*(triu(repmat(w,[n 1]),1).*triu(repmat(s,[n 1]),1))));

Oleg Komarov
Oleg Komarov 2012 年 3 月 27 日
I would however use a loop:
ws = w.*s;
wssw = bsxfun(@times,ws,ws.');
sum(wssw(~eye(n)))

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by