Eliminating outer for-loop

1 visualización (últimos 30 días)
Daniell Algar
Daniell Algar el 14 de Dic. de 2012
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

Respuesta aceptada

John Petersen
John Petersen el 14 de Dic. de 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.
  1 comentario
Daniell Algar
Daniell Algar el 16 de Dic. de 2012
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!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Variables en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by