MATLAB: Using the unique function to eliminate rows containing the same integers in a different order.

arrayconnectivityindexingMATLABmembersunique

Hi,
I have an N x 2 array containing integers as shown below.
D = [1 4; 1 5; 2 3; 2 4; 2 5; 3 2; 4 1; 4 2; 4 5; 5 1; 5 2; 5 4]
I wish to remove rows that repeat numbers such as D(1,:) = [1 4] and D(:,7) = [4 1]. In this case, I would want to remove D(:,7) from the array as row 1 already contains numbers 1 and 4.
I typically use the unique function to both sort and filter my data in cases like this, however due to the different indices I have not yet managed to get this method to work.
[members]=unique(D,'rows');
Note: My desired output matrix is as follows:
D = [1 4; 1 5; 2 3; 2 4; 2 5; 3 2]
Any guidance would be very much appreciated!
Sam

Best Answer

[~,x] = unique(sort(D,2),'rows','first');
result = D(x,:);