Path: news.mathworks.com!not-for-mail From: "Jveer " <jveer@jveer.com> Newsgroups: comp.soft-sys.matlab Subject: Re: please help optimize this ('find' is too slow) Date: Sat, 6 Dec 2008 21:36:03 +0000 (UTC) Organization: University of Manchester Lines: 34 Message-ID: <gher83$sfd$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> Reply-To: "Jveer " <jveer@jveer.com> 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 1228599363 29165 172.30.248.38 (6 Dec 2008 21:36:03 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Sat, 6 Dec 2008 21:36:03 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 1206797 Xref: news.mathworks.com comp.soft-sys.matlab:505397 hello Roger, thank you for the quick replies i've tried your latest recommendations before. I'm afraid they either dont work or dont provide any speed improvements at all 1. the && doesnt work with vectors. it gives the following error msg: "??? Operands to the || and && operators must be convertible to logical scalar values." 2. as for keeping the min,max outside the loop, i did the following: Uy=max(NCy,[],2)';Ly=min(NCy,[],2)';Uz=max(NCz,[],2)';Lz=min(NCz,[],2)'; (outside loop) and then changed the inside loop statements to these: ny=nx(P(2,nx)>=Ly(c)&P(2,nx)<Uy(c)); nz=ny(P(3,ny)>=Lz(c)&P(3,ny)<Uz(c)); however no incr in speed. the main drawback from this method is that i'll have FOUR additional very long variables coz the column length of P is typically more than 350k for any decent simulation. therefore from a memory optimization perspective it is not desirable. 3. the ~isempty test hardly takes any time. also in MOST CASES they are empty. this test is kinda my workout not being able to use short-circuit &&. removing the ~isempty tests dont increase speed. i understand your point that matlab understands how to deal with nx=[]. i included out of a 'paranoia' that matlab will run unnecessary of the inside brackets before realising that nx=[]; nx(P(2,nx)>=min(NCy(c,:))&P(2,nx)<max(NCy(c,:))); correction from my last post the function PinC runs the same amount of the time steps, not 600k- more like 200-800 typically. i was wondering if there any chance of using a similar method to the one u came up with to replace the 'find' function. i was well impressed by that and i still dont fully understand it. the 2 time consuming lines in my current function are: ny=nx(P(2,nx)>=min(NCy(c,:))&P(2,nx)<max(NCy(c,:))); nz=ny(P(3,ny)>=min(NCz(c,:))&P(3,ny)<max(NCz(c,:))); they both sort of do what that find function did except they use logicals. referring to an older version in the first post, i broke down that long logical so that the above two lines operate on subsets instead of the whole range of terms(typically 350k for every row) any other recommendations you may have are most welcome. "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <ghefbe$ell$1@fred.mathworks.com>... > "Jayveer " <jveer@jveer.com> wrote in message <gha1vj$850$1@fred.mathworks.com>... > I notice a couple of other aspects of your code that you could change to possibly improve its timing efficiency, Jayveer.