[SciPy-user] Ordering and Counting the Repetitions of the Rows of a Matrix
Stéfan van der Walt
stefan at sun.ac.za
Sun Jul 27 17:37:24 EDT 2008
2008/7/27 Warren Weckesser <warren.weckesser at gmail.com>:
> Lorenzo,
>
> Given a matrix A like you showed, here is one way to find (and count) the
> unique rows:
>
> ----------
> d = {}
> for r in A:
> t = tuple(r)
> d[t] = d.get(t,0) + 1
>
> # The dict d now has the counts of the unique rows of A.
>
> B = numpy.array(d.keys()) # The unique rows of A
> C = numpy.array(d.values()) # The counts of the unique rows
> ----------
And here's an evil one-liner:
x[np.sum(np.triu(np.all(x == x[:,None], axis=2)), axis=1)
that requires a lot of memory (given M rows of N columns, a temporary
array of MxMxN is used). It is blazingly fast, though :)
Cheers
Stéfan
More information about the SciPy-User
mailing list