Compare two arrays and print out Index of row in python

vapham

I have two arrays A and B

A = np.array([[9, 10, 11, 12.0],[13 14 15 16.3]])
B = np.array([[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12],[13, 14, 15, 16],[17, 18, 19, 20]])

I want to compare a couple of the elements of A[:,2:4] with couple elements of B[:,2:4] and print out the rows where they are equal or approximate? Here is my approach just for only A[:,3]

import math
import cmath
import numpy as np
A = np.array([[9, 10, 11, 12],[13, 14, 15, 16.3]])
B = np.array([[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12],[13, 14, 15, 16],[17, 18, 19, 20]])
for k in range(0,A.shape[0]):
    i = np.where(np.isclose(B[:,3], A[k,3], atol=0.5))
    print (i)

How can I do it with both A[:,2] and A[:,3] ?

Many thanks

ALai

I think you are looking for something like this:

import numpy as np

indexes_to_compare = range(2,4)
A = np.array([[9, 10, 11, 12],[13, 14, 15, 16.3]])
B = np.array([[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12],[13, 14, 15, 16],[17, 18, 19, 20]])

for a in A:
    print(np.where(np.all(np.isclose(B[:,indexes_to_compare], a[indexes_to_compare], atol=0.5), axis=1))[0])

You compare B with each row of A, taking into consideration only few indexes.

Thus you:

  • Check if B is close to a row of A (in selected indexes) with np.isclose(_)
  • Keep only rows where all selected indexes are close, with np.all(_, axis=1)
  • Get index of such rows with np.where

If you want to make sure to only output one row of B per cycle, just use np.where(_)[0][0]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to print out the substring between two "|" in python?

Python : Compare two csv files and print out differences

Compare two int arrays and print the missing numbers

How to compare two arrays and then return the index of the difference?

Compare two String[] arrays and print out the strings which differ

Compare two arrays with different size - python numpy

Compare two list with dicts and print out value that is not in the list using Python?

Python how to compare the elements in 2 arrays by row

Compare two dictionaries and print out missing or no matches

Python : How to compare two csv files and print out the matching strings in a new file

Perl (compare two file) print out differences on main file only

Compare two arrays and print number of matching elements

compare and print the values in two arrays using awk

compare two lists in python and print the difference

compare an array with two other arrays in python

Python: Compare Elements of two arrays

Compare two arrays in different columns in a file and print matching elements for every row using unix

Compare two arrays and find the index of uncommon elements in SWIFT 3

How to compare the contents of two arrays and returning the index of the duplicated file?

Compare two lists and print out when a change happens

How to compare two arrays by multiple columns (row with row)

Compare two arrays and at same value index change data

Compare numbers in two text documents and print out if the difference is too big

How to compare two arrays and eliminate duplicates python

Compare two columns and return the row numbers in Python

way to compare between index of two lists in python

Compare two numpy arrays and return matching nth element of row

Compare two arrays of objects and filter out similar property values?

How to compare two byte arrays in Python