Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I delete the intersection of sets A and B from A without sorting in MATLAB?

Two matrices, A and B:

A = [1 2 3
     9 7 5
     4 9 4
     1 4 7]

B = [1 2 3
     1 4 7]

All rows of matrix B are members of matrix A. I wish to delete the common rows of A and B from A without sorting.

I have tried setdiff() but this sorts the output.

For my particular problem (atomic coordinates in protein structures) maintaining the ordered integrity of the rows is important.

like image 837
Darren J. Fitzpatrick Avatar asked Aug 12 '10 12:08

Darren J. Fitzpatrick


2 Answers

Use ISMEMBER:

%# find rows in A that are also in B
commonRows = ismember(A,B,'rows');

%# remove those rows
A(commonRows,:) = [];
like image 117
Jonas Avatar answered Nov 20 '22 18:11

Jonas


I had to create diff between two arrays without sorting data. I found this great option in matlab docs. Setdiff function

Here is definition of function [C,ia] = setdiff(___,setOrder) If you do not want output data sorted use 'stable' otherwise 'sorted' or without parameter.

Here was my use case.

yDataSent = setdiff(ScopeDataY, yDataBefore, 'stable')
yDataBefore = ScopeDataY;
like image 24
Erich Stark Avatar answered Nov 20 '22 17:11

Erich Stark