Deleting repeated values in 2 matrices

4 views (last 30 days)
savas
savas on 31 Mar 2023
Commented: Cris LaPierre on 31 Mar 2023
Heya so I have a 22*2 and 25*2 matrix and I want to delete rows on both of them that have the same value on the 2nd column. So if theres say 1.25 on row 6 of A and 1.25 on row 8 of B is it possible to detect this and delete both rows?

Answers (2)

Cris LaPierre
Cris LaPierre on 31 Mar 2023
It's possible. Depending on what your requirements are, you might be interested in setdiff or ismember. Here's an example using ismember.
A = [rand(1,4);0:3]';
B = [rand(1,5);1:2:9]';
[indB,inA] = ismember(B(:,2),A(:,2))
indB = 5×1 logical array
1 1 0 0 0
inA = 5×1
2 4 0 0 0
B(indB,:) = []
B = 3×2
0.2212 5.0000 0.3219 7.0000 0.6421 9.0000
A(inA(indB),:) = []
A = 2×2
0.4994 0 0.2882 2.0000
  2 Comments
Dyuman Joshi
Dyuman Joshi on 31 Mar 2023
@Cris LaPierre, I would suggest to use ismembertol as OP is dealing with floating-point numbers.

Sign in to comment.


Dyuman Joshi
Dyuman Joshi on 31 Mar 2023
%logical indexing, use tolerance to compare floating point numbers
idx1 = abs(y1(:,2)-1.25)<1e-4);
idx2 = abs(y2(:,2)-1.25)<1e-4);
%deletion
y1(idx1,:)=[];
y2(idx2,:)=[];

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!