How would I find all rows of a matrix which contain only unique elements? For example, if I have a matrix containing all 3-way combinations of the numbers 1-3:
>> x = allcomb(1:3, 1:3, 1:3)
x =
1 1 1 1 1 2 1 1 3 1 2 1 1 2 2 1 2 3 1 3 1 1 3 2 1 3 3 2 1 1 2 1 2 2 1 3 2 2 1 2 2 2 2 2 3 2 3 1 2 3 2 2 3 3 3 1 1 3 1 2 3 1 3 3 2 1 3 2 2 3 2 3 3 3 1 3 3 2 3 3 3
I could do the following:
y = unique(sort(x, 2), 'rows'); y(y(:,1) == y(:,2) | y(:,1) == y(:,3) | y(:,2) == y(:,3), :) = [];
But is there a more efficient way to accomplish this for matrices with an arbitrary number of columns?
Thanks, Dan
Best Answer