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$>
References: <i4ob6h$67g$> <i4p2f2$cmu$> <i4prl7$idv$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1282444505 25103 (22 Aug 2010 02:35:05 GMT)
NNTP-Posting-Date: Sun, 22 Aug 2010 02:35:05 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: comp.soft-sys.matlab:664009

"James Morgan" <> wrote in message <i4prl7$idv$>...
> 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