Distance between all points in array and delete the second point if it is less that certian value (Victorized)
Show older comments
I have this loop to calculate the distance between all of the points in R_all array and delete the second point if the distace less that 0.002, but if I have a huge number of points like 100000 it takes long time, I need to vectorize my code, if you can help me ,, thank you in advance,,
Rx=rand(n,1)*0.2;
Ry=rand(n,1)*0.2;
Rz=rand(n,1)*0.2;
R_all=[Rx Ry Rz];
n= 1000;
Df=0.002;
while j<n
i=j+1;
while i<=n
k = norm(R_all(j,:)-R_all(i,:)); %function of distance between points
if k < 1.5*Df % check the distance between all points; should not be < 1.5 Df
R_all(i,:)=[];
n=n-1;
end
i=i+1;
end
j=j+1;
end
Accepted Answer
More Answers (1)
Something like this?
n= 1000;
Rx=rand(n,1)*0.2;
Ry=rand(n,1)*0.2;
Rz=rand(n,1)*0.2;
R_all=[Rx Ry Rz];
Df=0.002;
Dr = diff(R_all,[],1); % compute differences between each row
% changing the third input from 1 to 2 makes diff compute column differences instead
[i,j,k] = find(Dr<1.5*Df); % get indices of out-of-tolerance lines
i = i + 1; % Delete the second of the lines that makes an OOT result
R_all(i,:) = []; % remove lines that are out-of-tolerance
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!