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:56:02 +0000 (UTC)
Organization: University of Manchester
Lines: 30
Message-ID: <gh9qmi$l5c$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>
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 1228434962 21676 172.30.248.35 (4 Dec 2008 23:56:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 4 Dec 2008 23:56:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1206797
Xref: news.mathworks.com comp.soft-sys.matlab:505085


i changed my code to the following based on your recommendation. it produces a PiC full of zeros which implies its not going past the ifs. i must be doing something wrong. could you please have a run through it?

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