Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: find vector r<=p (p: a given vector)
Date: Mon, 19 Jan 2009 22:32:15 +0000 (UTC)
Organization: Battelle Energy Alliance (INL)
Lines: 12
Message-ID: <gl2v1f$cmg$1@fred.mathworks.com>
References: <gf2ppf$q6l$1@fred.mathworks.com> <ggcs55$aiv$1@fred.mathworks.com> <ggf7ro$fal$1@fred.mathworks.com> <gl26qm$8p1$1@fred.mathworks.com> <gl2tva$2ng$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1232404335 13008 172.30.248.37 (19 Jan 2009 22:32:15 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 19 Jan 2009 22:32:15 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 688530
Xref: news.mathworks.com comp.soft-sys.matlab:512573


Oh, hello.  I didn't see this old one had been resurrected!

Basically my approach was to build the index for cumsum (a technique I have seen many others use).  If you run the code without the semicolon on the line that creates the drops for y you can see the pattern.  To use this method you need to understand how cumsum works, and look for this type of pattern.  Since the result from cumsum is off by one, I just subtracted one from the vector before storing it in R.  

The main speed advantage, as far as loop is concerned, is pre-allocating the matrix R and y.  Also, Matlab's JIT likes all variables to stay the same size and type during a loop, so that is why I set y to zero at the end of each iteration.  

Hope that clears things up some :)




e!R](z882.'y%$8.{"X/F%}})&8&("?(} y-8yy(-y}(z{q'8(y8?-)y!8!