Path: news.mathworks.com!not-for-mail
From: "Jayveer " <jveer@jveer.com>
Newsgroups: comp.soft-sys.matlab
Subject: please help optimize this ('find' is too slow)
Date: Tue, 2 Dec 2008 20:40:04 +0000 (UTC)
Organization: University of Manchester
Lines: 35
Message-ID: <gh46f4$pg2$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 1228250404 26114 172.30.248.35 (2 Dec 2008 20:40:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 2 Dec 2008 20:40:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1206797
Xref: news.mathworks.com comp.soft-sys.matlab:504488


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