Path: news.mathworks.com!not-for-mail
From: "Jayveer " <jveer@jveer.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: please help optimize this ('find' is too slow)
Date: Thu, 4 Dec 2008 23:50:19 +0000 (UTC)
Organization: University of Manchester
Lines: 33
Message-ID: <gh9qbr$e6p$1@fred.mathworks.com>
References: <gh46f4$pg2$1@fred.mathworks.com> <gh7ror$eno$1@fred.mathworks.com> <gh7vs8$fgq$1@fred.mathworks.com>
Reply-To: "Jayveer " <jveer@jveer.com>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1228434619 14553 172.30.248.35 (4 Dec 2008 23:50:19 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 4 Dec 2008 23:50:19 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1206797
Xref: news.mathworks.com comp.soft-sys.matlab:505084

i've changed my code to the following based on your recommendation. it doesnt produce the same result as the original one :S. i think i might be doing something wrong. could you please check it?

i understand the idea up to the sorting but can t really see whats going witht the remaining 3 lines you wrote. your solution gives a PiC full of zeros i.e it does go past the ifs!

new code based on your recommendation: 

clear;load SampleData2.mat NCx NCy NCz P

m=length(NCx(:,1));
if m<=32767,PiC1=zeros(1,length(P),'int16');
else PiC1=single(zeros(1,length(P)));end

Lx=max(NCx,[],2)';Ux=min(NCx,[],2)'; % Generates lower and upper limits
n = size(P,2);
% m = length(Lx); % same as length(NCx(:,1));
[t,r] = sort([Lx,Ux,P(1,:)]);
s = 1:length(r); s(r) = s; % <-- s is the inverse of permutation r

% Simulataneously findind all Material Points that could be in cell c using
% the x,y and z limits of the cell
for c=1:m% For each cell
    nx = r(s(c):s(c+m));
    nx = nx(nx>2*m)-2*m;
%    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);PiC1(nz)=c;end
        end
    end
end