Remove all rows of an array based on a value in a column, and also remove all rows that have the same values as this row in other columns
2 views (last 30 days)
Show older comments
Hi, I have the centroids (x,y) of e.g 3 objects as shown in the pic:
I also have an array of the centroid locations (x,y) of each point and then calculate the distance of every other point from that point. Columns 1, 2 are (x,y) of an object, 3rd and 4th columns are coordinates of other objects, and column 5 is their seperation
M =
15.88 18.43 15.88 18.43 0
15.88 18.43 33.48 14.99 17.94
15.88 18.43 53.49 37.71 42.27
33.48 14.99 15.88 18.43 17.94
33.48 14.99 33.48 14.99 0
33.48 14.99 53.49 37.71 30.27
53.49 37.71 15.88 18.43 42.27
53.49 37.71 33.48 14.99 30.27
53.49 37.71 53.49 37.71 0
what I want to do is e.g. select a distance i.e. d=40, and then remove all rows in which column 5 (= their seperation) is above this value AND also any other row that has the same values in col 1 & 2. i.e. so to completely remove any object that is close to another object.
This is my attempt:
% Create another matrix where the condition is met
M1=M(M(:,5) < d, :);
disp('after')
M1
% Create conditions
TF1 = M(:,1)==M1(:,1)
TF1 = M(:,2)==M1(:,2)
% combine them
TFall = TF1 & TF2
% remove
M(TFall,:) = []
4 Comments
Accepted Answer
Bruno Luong
on 9 Aug 2022
M = [
15.88 18.43 15.88 18.43 0
15.88 18.43 33.48 14.99 17.94
15.88 18.43 53.49 37.71 42.27
33.48 14.99 15.88 18.43 17.94
33.48 14.99 33.48 14.99 0
33.48 14.99 53.49 37.71 30.27
53.49 37.71 15.88 18.43 42.27
53.49 37.71 33.48 14.99 30.27
53.49 37.71 53.49 37.71 0 ];
xy=M(:,1:2)
M(ismember(xy,xy(M(:,5)>40,1:2),'rows'),:)=[]
More Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!