Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: please help optimize this ('find' is too slow)
Date: Sun, 7 Dec 2008 04:42:01 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 21
Message-ID: <ghfk6p$mu6$1@fred.mathworks.com>
References: <gh46f4$pg2$1@fred.mathworks.com> <gh7ror$eno$1@fred.mathworks.com> <gh7vs8$fgq$1@fred.mathworks.com> <gh9faq$fb4$1@fred.mathworks.com> <gh9qmi$l5c$1@fred.mathworks.com> <gh9ske$6he$1@fred.mathworks.com> <gha1vj$850$1@fred.mathworks.com> <ghefbe$ell$1@fred.mathworks.com> <gher83$sfd$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1228624921 23494 172.30.248.38 (7 Dec 2008 04:42:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 7 Dec 2008 04:42:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:505432


"Jveer " <jveer@jveer.com> wrote in message <gher83$sfd$1@fred.mathworks.com>...
> .......
> i was wondering if there any chance of using a similar method to the one u came up with to replace the 'find' function.  .........

  Sorry about the '&&' operator - my mistake.  However, you can achieve the same effect by replacing

 ny = nx(P(2,nx)>=Ly(c) & P(2,ny)< Uy(c));

with

 ny = nx(P(2,nx)>=Ly(c));
 ny = ny(P(2,ny)< Uy(c));

The second line handles only those elements which passed the test in the first line, so there should be fewer comparisons made altogether.  On my machine the two lines are in fact generally somewhat faster than the single line (though my version is admittedly rather ancient and therefore an unreliable measure of newer execution times.)

  As to coming up with a fundamentally improved method of indexing on the 'ny' line, I'm afraid that might be very difficult to do.  This problem has come up in earlier threads and I was unable then to find a way of speeding up more than the first coordinate set computation.  And unfortunately I'm no smarter now than I was then.

  The problem is that when one has done the all-important sort on x-values, then the corresponding y-values are not in sorted order, and the method used depends heavily on this sorted property in quickly picking out the x's that were in range.  The corresponding y values can be all over the place and it's not easy to find those that also lie within the cell's lower and upper y-limits without tediously checking each one individually, as in the present code.

Roger Stafford