Skip to content Skip to sidebar Skip to footer

How Can I Find Identical X,y Coordinates In Two Arrays Of Corrdinates?

Given two 2D numpy arrays containing x and y coordinates, how can I find identical pairs in another array with identical dimensions? For example, I have these arrays: array([[ 2,

Solution 1:

Try this:

>>> a2 = [[ 0,  2,  3,  4],
   [ 3,  4, 11, 10]]
>>> a1 = [[ 2,  1,  3,  4],
   [ 4,  3,  5, 10]]
>>> set(zip(*a1)) & set(zip(*a2))
{(4, 10), (2, 4)}

You could traslate the array to list by array.tolist()

For any 2D array, to say, the first row represents the X-axis, and the second the Y-axis. So zip(*a1) would result in all coordinate pairs. Then the set() constructor will filter out all the duplicate records. And finally, the & operation between two set would figure out all the coordinate pairs, in both two arrays.

Hope it helps!

Solution 2:

The numpythonic way of doing this would be as follows:

>>> a1 = np.array([[2, 1, 3, 4], [4, 3, 5, 10]])
>>> a2 = np.array([[0, 2, 3, 4], [3, 4, 11, 10]])
>>> a1 = a1.T.copy().view([('', a1.dtype)]*2)
>>> a2 = a2.T.copy().view([('', a2.dtype)]*2)
>>> np.intersect1d(a1, a2)
array([(2, 4), (4, 10)], 
      dtype=[('f0', '<i4'), ('f1', '<i4')])

Solution 3:

A direct solution would be:

import numpy

array1 = numpy.array([[ 1, 99, 2, 400],
                      [ 3, 98, 4, 401]])

array2 = numpy.array([[ 1,  6, 99,   7],
                      [ 8,  9, 98, 401]])

result = []
for column_1 in xrange(array1.shape[1]):
    for column_2 in xrange(array2.shape[1]):
        if numpy.array_equal(array1[:,column_1], array2[:,column_2]):
            result.append(array1[:,column_1])

print numpy.array(result).transpose()

[[99]
 [98]]

Post a Comment for "How Can I Find Identical X,y Coordinates In Two Arrays Of Corrdinates?"