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: Tue, 2 Dec 2008 21:25:03 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 41
Message-ID: <gh493f$j96$1@fred.mathworks.com>
References: <gh46f4$pg2$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 1228253103 19750 172.30.248.37 (2 Dec 2008 21:25:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 2 Dec 2008 21:25:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 116572
Xref: news.mathworks.com comp.soft-sys.matlab:504497


Some sample data would be helpful.

Regards,
Stefan

"Jayveer " <jveer@jveer.com> wrote in message <gh46f4$pg2$1@fred.mathworks.com>...
> can anyone please help me speed this up?the line with find is way too slow!
> 
> any suggestions would be much appreciated.
> 
> Description of inputs:
> P: 3Xn matrix where each row represents x,y and z coordinates of P(n)
> NCx,NCy,NCz: mX8 matrix each where each of the 8 columns represent the coordinate (x or y or z depending NCx or NCy or NCz) of nodes one to 8 of a cubic cell. 
> 
> function [PiC]=PinC(P,NCx,NCy,NCz)
> if length(NCx(:,1))<=32767,PiC=zeros(1,length(P),'int16');
> else PiC=single(zeros(1,length(P)));end
> % Simulataneously finding all Material Points that could be in cell c using
> % the x,y and z limits of the cell
> for c=1:length(NCx(:,1))% For each cell
>     nx=find(P(1,:)>=min(NCx(c,:))&P(1,:)<max(NCx(c,:))); % bottleneck!
>     if ~isempty(nx);
>         ny=nx(P(2,nx)>=min(NCy(c,:))&P(2,nx)<max(NCy(c,:)));
>         if ~isempty(ny);
>             nz=ny(P(3,ny)>=min(NCz(c,:))&P(3,ny)<max(NCz(c,:)));
>             if ~isempty(nz);PiC(nz)=c;end
>         end
>     end
> end
> 
> ---------older version - a lot slower!!!
> function [PiC]=PinC(P,NCx,NCy,NCz)
> PiC=zeros(1,length(P));
> % Simulataneously findind all Material Points that could be in cell c using 
> % the x,y and z limits of the cell
> for c=1:length(NCx(:,1))% For each cell    
>     n=logical(P(1,:)>=min(NCx(c,:))&P(1,:)<max(NCx(c,:))&P(2,:)>=min(NCy...
>         (c,:))&P(2,:)<max(NCy(c,:))&P(3,:)>=min(NCz(c,:))&P(3,:)<max...
>         (NCz(c,:)));PiC(n)=c;
> end