I have two matrices shown below:
A =
0.2247 0.5845
0.4942 0.7303
0.5000 0.5000
0.6440 0.5408
0.6210 0.2230
0.6210 0.2230
0.2869 0.3921
1.0000 1.0000
B =
0.2247 0.5845
0.4942 0.7303
0.6440 0.5408
0.6210 0.2230
0.2869 0.3921
I would like to delete the rows in matrix A that match the rows from matrix B. How would I go about doing this?
Thanks in advance.

 채택된 답변

Adam Danz
Adam Danz 2019년 11월 23일
편집: Adam Danz 2019년 11월 24일

0 개 추천

If you're looking for exact matches use
However, with floating decimals it's likely that your variable values may differ slightly such as 0.28690123 vs 0.28690131. If you'd like that to be considered a match, use
To identify and remove the first match of B within A,
removeRowIdx = cell2mat(arrayfun(@(i) find(ismember(A,B(i,:),'rows'),1,'first'),(1:size(B,1))','UniformOutput',false));
A(removeRowIdx,:) = []

댓글 수: 4

John D
John D 2019년 11월 24일
편집: John D 2019년 11월 24일
Thanks, I'd be looking for exact matches.
I wanted this to find and remove only the first occurrence of each. Would this do that, or remove all occurrences? I've updated the example data.
Adam Danz
Adam Danz 2019년 11월 24일
Hi John, I added two lines to the end of my answer that identifies and removes only the first matching row. This uses ismember() so the match must be a perfect match. With floating decimals you may want to use ismembertol() instead if you find that number that seem like perfect matches aren't being matched.
John D
John D 2019년 11월 24일
Thanks, that helps
Adam Danz
Adam Danz 2019년 11월 24일
Glad I could help!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2019a

질문:

2019년 11월 23일

댓글:

2019년 11월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by