Compare two arrays with different size - python numpy

CardCaptor

So i have two arrays, they have the same dimension but different lenght.

Arr1 = np.array([[Ind1],[Ind2],[Ind3]])

Arr2 = np.array([[Ind7],[Ind3],[Ind3],[Ind4]])

I need to get the position and value of the elements that have the same position and are equal in both arrays.

In the example case the expected answer will be:

Position = 2

Value = Ind3

I'm using python with the numpy module.

Divakar

With NumPy arrays, you might want to work in a vectorized manner for performance and also to make use of array-slicing. With that in mind, here's one approach for input arrays a and b -

n = min(len(a), len(b))
out_idx = np.flatnonzero(a[:n] == b[:n])
out_val = a[out_idx] # or b[out_idx] both work

This takes care of multiple matches.

Sample run -

In [224]: a = np.array([3, 8, 9, 2, 1, 7])

In [225]: b = np.array([1, 2, 9, 7, 5, 7, 0, 4])

In [226]: n = min(len(a), len(b))
     ...: out_idx = np.flatnonzero(a[:n] == b[:n])
     ...: out_val = a[out_idx]
     ...: 

In [227]: out_idx
Out[227]: array([2, 5])

In [228]: out_val
Out[228]: array([9, 7])

For a list of tuples as output for indices and their values -

In [229]: zip(out_idx, out_val)
Out[229]: [(2, 9), (5, 7)]

For a pretty dictionary output of indices and corresponding values -

In [230]: {i:j for i,j in zip(out_idx, out_val)}
Out[230]: {2: 9, 5: 7}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Compare two arrays in Numpy

Compare two unequal size numpy arrays and fill the exclusion elements with nan

how to compare two arrays with different size using php?

Compare elements in two 1D arrays of different size, with tolerance

Compare numpy arrays of different shapes

compare two dicts with numpy arrays

Python, compare two numpy arrays, find groups and remove duplicates

possible unique combinations between between two arrays(of different size) in python?

Python Add Two Different Numpy Arrays To Nested Dictionaries

Broadcasting over two arrays with different shapes (Numpy-Python)

Search for duplicated rows in two numpy arrays with different length (Python)

combined two arrays of different size with different properties

Python: Compare Elements of two arrays

how to compare two numpy arrays with unequal length

PHP - compare and filter two arrays with different dimensions

Compare two different length arrays using lodash

Twig compare two values in different arrays

Compare two arrays of different lengths and exclude matches

Compare two fields from different arrays in mongodb

How to compare two different arrays without methods?

How to compare objects of two different arrays in javascript

How to compare two different arrays with common properties

Compare two pandas dataframe with different size

Compare two different size vectors of the same struct

Compare two dataframes of different size on multiple conditions

Compare coordinates of two images of different size

Operation on numpy arrays contain rows with different size

How to compare the size of two times in Python, if two times have different sources?

How to compare the size of two arrays and return a larger array(not value or size)?