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

1 view (last 30 days)
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
Jason
Jason on 9 Aug 2022
Matt, it was towards the same goal yes, but I realised that finding the distance of all objects and simply finding rows where this is below a set value doesn't quite work - but it did answer the main question - how to find distance of all objects!. Yes I could have added this problem in a comment there too, but this is more of a general matrix manipulation problem (i.e. use column 5 to flag the values in columns 1 & 2 and then put remove all rows that have these col 1& 2 values. I thought as its a different Matlab problem it would be easier to search for as well so I created a new thread.

Sign in to comment.

Accepted Answer

Bruno Luong
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)
xy = 9×2
15.8800 18.4300 15.8800 18.4300 15.8800 18.4300 33.4800 14.9900 33.4800 14.9900 33.4800 14.9900 53.4900 37.7100 53.4900 37.7100 53.4900 37.7100
M(ismember(xy,xy(M(:,5)>40,1:2),'rows'),:)=[]
M = 3×5
33.4800 14.9900 15.8800 18.4300 17.9400 33.4800 14.9900 33.4800 14.9900 0 33.4800 14.9900 53.4900 37.7100 30.2700
  1 Comment
Jason
Jason on 9 Aug 2022
Thankyou, I modified it slightly to remove the rows with the last coloumn as zero and it worked beautifully
TF1 = M(:,5)==0;
M(TF1,:) = []

Sign in to comment.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by