removing three loops which are predeterminded

1 visualización (últimos 30 días)
som
som el 31 de Mzo. de 2012
Hi all
I've writen a program including four loops and they all located inside a "while" loop as below:
q=[0,10,20; 40,50,60; 10,20,30;] ;
s=[0;10;20;30];
r=[10;20;30;40];
nq=size(q,2);
ns=length(s);
nr=length(r);
n=1;
while n<10000
for t=3:-1:1
for ss=1:ns
for qq=1:nq
for rr=1:nr
s2{t,ss}(qq,rr)= s(ss)+q(t,qq)-r(rr);
if s2{t,ss}(qq,rr)<=100 && s2{t,ss}(qq,rr)>=0
tsd{t,ss}(qq,rr)=10^25;
else
s2{t,ss}(qq,rr)=-1e6;tsd{t,ss}(qq,rr)=-1e25;
end
end
end
end
n=n+1;
end
end
since the values of "ss", " qq" and "rr" are predeterminded, I want to vectorize above programand and finally decrease the number of loops by removing "ss","qq" and "rr" loops, respectively.
How can I remove three mentioned loops in the way all results related to "s2" and "tsd" be visible sepratly. any help would be appreciated.
  10 comentarios
Walter Roberson
Walter Roberson el 31 de Mzo. de 2012
som, if you create answers faster if the question contains an "urgent", then that must be on some other forum, as you have not answered any questions at all here.
Jan
Jan el 1 de Abr. de 2012
@Walter: "Faster" can be related to an event in the future also.

Iniciar sesión para comentar.

Respuesta aceptada

Jan
Jan el 31 de Mzo. de 2012
I do not see a method to avoid all loops. And even if this is possible, it is not guaranteed to be more efficient.
Anyhow, you can improve the current version:
s2 = cell(3, ns); % Pre-allocate!
tsd = cell(3, ns);
while n<10000
for t = 3:-1:1
a = bsxfun(@minus, q(t, :)', r);
for ss = 1:ns
b = s(ss) + a;
index_out = b > 100 | b < 0;
% index_in is not needed!
% index_in = s2{t,ss} <= 100 & s2{t,ss} >= 0;
% But if it is needed: index_in = not(index_out);
b(index_out) = -1e6;
s2{t, ss} = b;
tsd{t, ss}(index_out) = -1e25;
end
n = n+1;
end
end
  2 comentarios
som
som el 31 de Mzo. de 2012
thanks.In your idea there is no solution for removing all three loops containing "ss", " qq" and "rr" loops?
thanks
Jan
Jan el 1 de Abr. de 2012
No, I did not try to remove all loops, because I do not assume, that this will be an improvement. In general the removing of a loop requires the creation of an temporary array. The larger the temporary arrays, the smaller is the benefit of vectorizing. With pre-allocation a FOR-loop can be the fastest method.

Iniciar sesión para comentar.

Más respuestas (1)

som
som el 3 de Abr. de 2012
Dear Jan Simon,
since writing of above program by keeping just one loop i.e. " t loop" is so essential for me and on the other hand it's a main part of my project, could you please try for it and give me your valuable feedback. thanks
  1 comentario
Jan
Jan el 3 de Abr. de 2012
Dear som, I'm convinced that a fully vectorized method is slower than the shown code, especially if you store the data in form of cells in s2 and tsd. I cannot understand, why you want to avoid the loop and in consequence I cannot guess the requirements of the new code.
There is still a potential for further accelerations in the code. E.g.:
b = s(ss) + a;
index_out = b > 100 | b < 0;
can be expressed as:
index_out = abs(s(ss) + a - 50) > 50;
However, without knowing what you actually want to achieve, further suggestions are guesswork only.

Iniciar sesión para comentar.

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by