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

馋奶兔 提交于 2019-12-01 16:21:07

问题


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.


回答1:


Use ISMEMBER:

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

%# remove those rows
A(commonRows,:) = [];



回答2:


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;


来源:https://stackoverflow.com/questions/3467670/how-do-i-delete-the-intersection-of-sets-a-and-b-from-a-without-sorting-in-matla

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!