Eliminating outer for-loop
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Akzeptierte Antwort
John Petersen
am 14 Dez. 2012
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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Variables finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!