Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Possible vectorization of for loop?
Date: Sun, 22 Aug 2010 02:35:05 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 8
Message-ID: <i4q2cp$ogf$1@fred.mathworks.com>
References: <i4ob6h$67g$1@fred.mathworks.com> <i4p2f2$cmu$1@fred.mathworks.com> <i4prl7$idv$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1282444505 25103 172.30.248.35 (22 Aug 2010 02:35:05 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 22 Aug 2010 02:35:05 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:664009

"James Morgan" <martinfalch@hotmail.com> wrote in message <i4prl7$idv$1@fred.mathworks.com>...
> It did! And it cut off 80% of my computation time! :D Thank you so much for that suggestion!
- - - - - - - - - -
  I suspect the savings in cpu time are due primarily to avoiding repetition in taking the powers of X values rather than to the vectorization.  In the twenty-five trips through the previous for-loop the same power of each X element was being taken twenty-four times and only once with a different power.  I think if you first computed prod(bsxfun(@power,X,n_1))) and then did a multiplication and summing of this with each separate row of X using a for-loop, you would still save time.

  I am saying this so you won't put too much reliance on vectorization for its own sake.  For-loops, used properly, have an important rote to play in matlab coding, and they are sometimes the only way possible to carry out an algorithm.

Roger Stafford