필터 지우기
필터 지우기

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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by