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$>
References: <itoibv$e5j$> <itoisp$fdh$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1308611104 17751 (20 Jun 2011 23:05:04 GMT)
NNTP-Posting-Date: Mon, 20 Jun 2011 23:05:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: comp.soft-sys.matlab:732878

"John " <> wrote in message <itoisp$fdh$>...
> "John " <> wrote in message <itoibv$e5j$>...
> > 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