From: "Jayveer " <>
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$>
References: <gh46f4$pg2$> <gh7ror$eno$> <gh7vs8$fgq$>
Reply-To: "Jayveer " <>
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: 1228434619 14553 (4 Dec 2008 23:50:19 GMT)
NNTP-Posting-Date: Thu, 4 Dec 2008 23:50:19 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1206797
Xref: 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

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);
        if ~isempty(ny);
            if ~isempty(nz);PiC1(nz)=c;end