Skip to content

Instantly share code, notes, and snippets.

@GMadorell
Created May 17, 2014 19:25
Show Gist options
  • Save GMadorell/d4dfd283284fa537d3a3 to your computer and use it in GitHub Desktop.
Save GMadorell/d4dfd283284fa537d3a3 to your computer and use it in GitHub Desktop.
Find unique rows (submatrices) in a given matrix.
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