[Numpy-discussion] Numpify this?
Robert Kern
robert.kern@gmail....
Sun May 18 02:19:18 CDT 2008
On Sun, May 18, 2008 at 2:04 AM, Matt Crane <matt@snapbug.geek.nz> wrote:
> Hey,
>
> I'm new to numpy but not new to python or programming in general. I
> was wondering if there's a way of using numpy to do the following or
> whether I've got what I've got and that's as good as it's going to
> get.
>
> I have two 2d arrays and I want to create another 2d array that
> contains the values from the 2nd column of the first two arrays where
> the values in the 1st column match. To elaborate with an example - if
> I had an array a:
>
> array([[2834, 1], [3282, 3], [6850, 2], [9458, 2]])
> and an array b:
>
> array([[2834, 3], [3282, 5], [4444, 5], [9458, 3], [9999, 4], [11111,
> 5], [12345, 1]])
>
> then I'd want the result to be
>
> array([[1, 3], # from 2834
> [3, 5], # from 3282
> [2, 3]]) # from 9458
Are the matching rows always going to be the same row in each? I.e.
you want rows i such that a[i,0]==b[i,0] rather than trying to find
all i,j such that a[i,0]==b[j,0]?
If so, then I would do the following:
In [1]: from numpy import *
In [2]: a = array([[2834, 1], [3282, 3], [6850, 2], [9458, 2]])
In [3]: b = array([[2834, 3], [3282, 5], [4444, 5], [9458, 3], [9999,
4], [11111,
...: 5], [12345, 1]])
In [4]: minlength = min(a.shape[0], b.shape[0])
In [5]: matching = nonzero(a[:minlength,0] == b[:minlength,0])[0]
In [6]: matching
Out[6]: array([0, 1, 3])
In [7]: column_stack([a[matching,1], b[matching,1]])
Out[7]:
array([[1, 3],
[3, 5],
[2, 3]])
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the Numpy-discussion
mailing list