Eliminating outer for-loop

1 回表示 (過去 30 日間)
Daniell Algar
Daniell Algar 2012 年 12 月 14 日
Hi,
I have an outer for-loop that I can't manage to get rid of. Code as follows
for t= 1: epochs
... code
for i= 1: n
for j = 1:n
h= exp(-sum(([i j]'- [r c]').^2)/(2*(sigma(t)^2)));
w(:, i, j)= w(:, i, j)+ eta(t)*h*((xiCurrent- w(:, i, j)));
dist(i, j)= sqrt(sum((xiCurrent- w(:, i, j)).^2));
end
end
... code
end
Observe that in the loop over i nothing gets calculated before entering the loop over j, and I would therefore like to eliminate the i-loop, but with no success.
Constraint's are that one xiCurrent are fed per iteration t, and all computations within the t-loop must be finished before t can take a step. An indexed-value of w must be updated before being applied to dist(i, j).
If necessary; n= 30; size(w)= 57000 x 30 x 30; size(xiCurrent)= 57000 x 1 size(dist)= 30 x 30 r and c are constants in the context.
I've used e.g. repmat on xiCurrent, but that gave slower execution than the above code.
I'm grateful for any help, best regards, Daniell

採用された回答

John Petersen
John Petersen 2012 年 12 月 14 日
I don't see any recursion going on. So I don't see why you can't compute it without any loops. To compute h, try
I = ones(n)*diag([1:n]);
J = I';
I = I(:);
J = J(:);
h = exp(-sum(([I-r J-c]).^2)./(2*(sigma(t).^2)));
Not sure about w. Is it initialized? You should be able to do the same kind of thing with w and dist.
w(:, I, J)= w(:, I, J)+ eta(t).*h.*((xiCurrent- w(:, I, J)));
dist(I, J)= sqrt(sum((xiCurrent- w(:, J, J)).^2));
Also, if xiCurrent is not recursive either, then you might be able to eliminate the t loop as well.
  1 件のコメント
Daniell Algar
Daniell Algar 2012 年 12 月 16 日
Thank you very much for the answer. Apparently the "email notification on comments/answers" doesn't seem to do what I want it to, so sorry for the late reply.
That is a neet usage of the same approach I've tested earlier, thank you. I'm not able to test if you're suggestion works better then mine, because the matrices eats up all the memory I have (8Gb, where the free 4Gb on previously run got swallowed completely).
I believe you answered my question at least, so thank you sir!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeVariables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by