Using Logical Indexing with Matrices

5 views (last 30 days)
Andrew
Andrew on 6 Feb 2014
Edited: Matt J on 6 Feb 2014
Hello,
I am currently working on something where I have a large number of three dimensional points (8.9e7 points) expressed in a matrix like X=[x;y;z] where x, y, and z are row vectors containing the Cartesian coordinates of each point.
I need to search these points and pull out sets of points that fall on a given plane with a tolerance and store these points to a new matrix like X2=[x2;y2;z2] where x2, y2, and z2 are points from X that fall on the defined plane.
I have tried something like this:
%generating spherical point cloud
n=8937*10000;
L=sqrt(n*pi());
k=1:n;
z=ones(size(k))-(2*k-ones(size(k)))./n;
phi=acos(z);
theta=L*phi;
x=sin(phi).*cos(theta);
y=sin(phi).*sin(theta);
rbody=1737; alt=50;
X=[x*rbody;y*rbody;z*rbody];
%defining plane
ns=[1,1,1]; %normal vector
planeloc=rbody^2/(alt+rbody); %distance from origin
%Storing values that fit on the plane
X2=X(ns*(X-planeloc*ns'*ones(1,length(x)))>=-0.0001 & ns*(X-planeloc*ns'*ones(1,length(x)))<=0.0001);
max(max(X2)) %testing to see if any values fit
but it does not perform what I want. When I use the logical index
X(ns*(X-planeloc*ns'*ones(1,length(x)))>=-0.0001 & ns*(X-planeloc*ns'*ones(1,length(x)))<=0.0001)
because the index returns values for each element of the matrix. Is there some way that I can use logical indices to select the columns of X that satisfy
(ns*(X-planeloc*ns'*ones(1,length(x)))>=-0.0001 & ns*(X-planeloc*ns'*ones(1,length(x)))<=0.0001)
without running a for loop (which would obviously take forever due to the huge number of points).
Thanks, Andrew

Accepted Answer

Matt J
Matt J on 6 Feb 2014
Edited: Matt J on 6 Feb 2014
tmp=ns*X-planeloc*norm(ns)^2;
X2=X(:,abs(tmp)<.0001);

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!