How can I find unique rows in a matrix, with no element order within each row?

后端 未结 4 724
一整个雨季
一整个雨季 2020-12-10 09:30

I have an array comprising n rows and 4 colums. Each of the four entries on the row is an integer, i.e.,

X = [
       111 112 432   2
         6   9 115 111
         


        
相关标签:
4条回答
  • 2020-12-10 09:58

    First, sort the rows of your matrix to arrive at a "canonical" representation for the tetrahedra:

    X = sort(X, 2);
    

    Then, use unique with the optional 'rows' argument to find unique rows:

    Y = unique(X, 'rows');
    
    0 讨论(0)
  • 2020-12-10 09:59

    you should sort the rows first, then use unique(A,'rows') as HPM suggests

    0 讨论(0)
  • 2020-12-10 10:08

    unique() will work on rows, but rows 1 and 3 are a different order. So we could sort them prior to using unique.

    Y=unique(sort(X,2),'rows')
    
    Y =
    
         2   111   112   432
         6     9   111   115
    

    If you want to retain the original ordering then unique will return the indices

    [Y,yi]=unique(sort(X,2),'rows');
    
    >> X(yi,:)
    
    ans =
    
       112   432   111     2
         6     9   115   111
    
    0 讨论(0)
  • 2020-12-10 10:16

    To quote from the documentation:

    b = unique(A, 'rows') returns the unique rows of A.

    Is that what you want ?

    0 讨论(0)
提交回复
热议问题