how to compare to a row in matlab
I have a matrix similar to this one:
1468 1468
1468 1711
1468 1469
1711 1468
1711 1711
1711 1469
1469 1468
1469 1711
1469 1469
and I would like to remove the entry (i,j) iff开发者_如何学编程 the entry (j,i) exists. For example, I would like to remove 1711 1468
as 1468 1711
appears above it.
How do I do this?
Assuming that the order doesn't matter for removing rows, you can combine SORT and UNIQUE like this:
m = [1468 1468
1468 1711
1468 1469
1711 1468
1711 1711
1711 1469
1469 1468
1469 1711
1469 1469]
[~,v]=unique(sort(m,2),'rows');
trimmedM = m(sort(v),:) %# keep the row ordering as in the original
trimmedM =
1468 1468
1711 1468
1711 1711
1469 1468
1469 1711
1469 1469
Note that unique
will retain the last of the duplicate entries, which seems to fit with your requirements.
I assume by the nature of the problem that order is irrelevant. Then you could just store the smaller number in the first column and look for duplicate rows. This question has some suggestions for that. This line will perform that sort so the referenced methods will work:
x = [min(x(:,1),x(:,2)),max(x(:,1),x(:,2))]
精彩评论