Select the closest values ​​from two different arrays

Suppose I have a numpy array

A = [[1 2 3] [2 3 3] [1 2 3]] 

and another array

 B = [[3 2 3] [1 2 3] [4 6 3]] 

and an array of true values:

 C = [[1 4 3] [8 7 3] [4 10 3]] 

Now I want to create an array D, the elements of which will depend on A or B, and the condition is the closest value to each element from the array C.

Is there any pythonic way to do this? Im is using loops right now

+4
source share
1 answer
 >>> K = abs(A - C) < abs(B - C) # create array of bool [[True, False, False], [True, True, False], [False, False, False]] >>> D = where(K, A, B) # get elements of A and B respectively 
+7
source

Source: https://habr.com/ru/post/1501441/


All Articles