Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
832 views
in Technique[技术] by (71.8m points)

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

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
       112 432 111   2

    ]; 

Each row represents the vertices of a tetrahedron. These vertices have no directionality thus, in the case above, the tetrahedra represented by X(1,:) and X(3,:) are equivalent.

I wish to remove duplicate tetrahedra from X, but can't quite figure how to incorporate the order independence into my code.

I tried the UNIQUE() function but this returns a (nx1) array of unique integers, i.e.,

Y = UNIQUE(X);

Y = [
     2
     6
     9
     111
     112
     115
     432
    ]

Anyone have any suggestions for a reasonably efficient way to complete this task?

Thanks, S :-)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...