removing three loops which are predeterminded
Afficher commentaires plus anciens
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 commentaires
Walter Roberson
le 31 Mar 2012
http://www.mathworks.com/matlabcentral/answers/29922-why-your-question-is-not-urgent-or-an-emergency
Walter Roberson
le 31 Mar 2012
What is the difference between this question and your previous and still active question http://www.mathworks.com/matlabcentral/answers/33965-removing-three-loops-which-are-predeterminded
Jan
le 31 Mar 2012
@somayeh: The term "urgent" discourage others from answering.
Walter Roberson
le 31 Mar 2012
Yup. For example I'm going to have a shower instead of thinking about it.
Jan
le 31 Mar 2012
Dear Walter, I personally appreciate your brain. I do not hesitate to assume, that it can solve such problems even during having your shower. But a really urgent question would encourage you to even type in the answer from inside your shower stall.
som
le 31 Mar 2012
Jan
le 31 Mar 2012
Please do not take the discussion about "urgent" personally. You participated in this forum for 285 day now, asked a lot of questions, know how to format code and how to accept an answer. Then one can assume, that you have seen the 2nd most voted thread (see Walter's 1st comment) already.
I believe you, that a fast answer is essential for you. At the same time I'm convinced, that this is not essential for any of the voluntary contributors of this forum, because they (we) spend their spare time here. Therefore any kind of "as soon as possible" does not really match.
Do *you* create answers faster, if the question contains an "urgent"?!
However, you have edited your question. Beside remove the discussed term, you have removed your partially vectorized version. Why? Does my answer loose its meaning also, such that I should delete it?
som
le 31 Mar 2012
Walter Roberson
le 31 Mar 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
le 1 Avr 2012
@Walter: "Faster" can be related to an event in the future also.
Réponse acceptée
Plus de réponses (1)
som
le 3 Avr 2012
0 votes
1 commentaire
Jan
le 3 Avr 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.
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!