Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Speed of "for" loops
Date: Mon, 20 Jun 2011 23:05:04 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 17
Message-ID: <itojn0$han$1@newscl01ah.mathworks.com>
References: <itoibv$e5j$1@newscl01ah.mathworks.com> <itoisp$fdh$1@newscl01ah.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-05-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1308611104 17751 172.30.248.37 (20 Jun 2011 23:05:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 20 Jun 2011 23:05:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:732878

"John " <johnps@gmail.com> wrote in message <itoisp$fdh$1@newscl01ah.mathworks.com>...
> "John " <johnps@gmail.com> wrote in message <itoibv$e5j$1@newscl01ah.mathworks.com>...
> > Is there something peculiarly slow about using "for" loops? I have several "for" statements within each other (is this called "nested"?). I have estimated that it needs to perform a calculation involving trig functions roughly 1.12 * 10^10 times. 
> > 
> > Should this really be taking over half an hour? Any hints? I am wondering if it is possible to "vectorize" some of my code. 
> > 
> > Please bear in mind I'm new to Matlab. 
> 
> Suppose that I want to "vectorize" to make things faster-- if that does indeed make it faster. Suppose F1, A1, F2, A2, F3, A3, F4, and A4 are all vectors. (Fx is a calculated value). I want to calculate every single possibility, which can be done with loops. Why won't the following code work instead? It says inner dimensions must agree. Can this only be done with loops?
> 
> thrustx =
>                     F1.*cos(A1)+F2.*cos(A2)+F3.*cos(A3)+F4.*cos(A4)+
>                     Fx;
- - - - - - -
  I would advise you to give us the specifics of your problem.  Show us the nested for loops and the sizes of vectors involved.  Vague statements such as "calculate every single possibility" don't help much.

Roger Stafford