Created
May 17, 2014 19:25
-
-
Save GMadorell/d4dfd283284fa537d3a3 to your computer and use it in GitHub Desktop.
Find unique rows (submatrices) in a given matrix.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
# http://stackoverflow.com/questions/16970982/find-unique-rows-in-numpy-array | |
def find_unique_rows(array): | |
b = array[np.lexsort(array.reshape((array.shape[0],-1)).T)]; | |
return b[np.concatenate(([True], np.any(b[1:]!=b[:-1], axis=tuple(range(1,array.ndim)))))] | |
if __name__ == "__main__": | |
test_array = np.array([[0, 0], | |
[0, 1], | |
[1, 1], | |
[1, 1], | |
[0, 1], | |
[0, 0]]) | |
unique_elements = find_unique_rows(test_array) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment