# [SciPy-user] Ordering and Counting the Repetitions of the Rows of a Matrix

Warren Weckesser warren.weckesser@gmail....
Sun Jul 27 14:46:29 CDT 2008

```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
----------

For a large number of rows (e.g. 10000), this appears to be significantly
faster than the code that David Kaplan suggested in his email earlier today.

Regards,

Warren

On Sun, Jul 27, 2008 at 12:17 PM, Lorenzo Isella
<lorenzo.isella@gmail.com>wrote:

> Dear All,
> Consider an Nx2 matrix of the kind:
>
> A=   1 2
>       3 13
>       1  2
>       6  8
>       3 13
>       2  9
>       1  1
>
>
> The first entry in each row is always smaller or equal than the second
> entry in the same row.
> Now there are two things I would like to do with this A matrix:
> (1) With a sort of n.unique1d (but have not been very successful yet),
> let each row of A appear only once (i.e. get rid of the repetitions).
> Therefore one should obtain the matrix:
> B=   1 2
>       3 13
>       6  8
>       2  9
>       1  1
>
> (2) At the same time, efficiently count how many times each row of B
> appeared in A. I would like to get a C vector counting them as:
>
> C=   2
>       2
>       1
>       1
>       1
>
>
> Any suggestions about an efficient way of achieving this?
> Many thanks
>
> Lorenzo
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/scipy-user/attachments/20080727/c0f97b35/attachment.html
```