2

I have one array of vectors (or for simplicity you can assume I have one array) based on some computation I make 2 different arrays with same size of first array that contains number between 0 and 1 (for each index of the first array the second and third array containing the hamming distances) now I want to sort first array based on second and third array at the same time. Since second and third array contains value between 0 and 1 I want to sort first array in order to each element has the closest distance of second and third array to the value 0.5.

If I want to make some realistic example :

first = numpy.array([1,2,3])    
second = numpy.array([0.2, 0.5, 0.97])   
third = numpy.array([0.3, 0.45, .98])

first = sort(first, second, third)

After that first must be something like this: [2,1,3] why I should have this? because second[1] and third[1] are the closest point to the (0.5,0.5) (by closes I mean something like euclidean distance or any other one) and after index number 2 index number 0 has the second closes distance to point (0.5,0.5).

Or If I have second = numpy.array([0.62, 0.61, 0.97]) and third = numpy.array([0.49, 0.72, 0.97]) I want the sort array return indices like [0,1,2] so the first array after sorting is like this [1,2,3] why? because second[0] and third[0] are the closest point to the point (0.5,0.5) and so on.

1 Answer 1

2
import numpy as np

first = np.array([1,2,3])
second = np.array([0.2, 0.5, 0.97])
third = np.array([0.3, 0.45, .98])

sqdist = (second-0.5)**2 + (third-0.5)**2
idx = np.argsort(sqdist)
first = first[idx]
print(first)

yields

[2 1 3]
Sign up to request clarification or add additional context in comments.

6 Comments

I don't think this solution solve my problem because the second an third array has the same value for me,
Can you give another (longer?) example of first, second, third and the desired result?
for example if I have second = numpy.array([0.62, 0.61, 0.97]) and third = numpy.array([0.49, 0.72, 0.97]) your solution will return [1,0,2] but I want to return [0,1,2]
I don't know how to clear my question in order to make myself understood, you must assume the second and third array as a same value key I want to sort first array somehow that each equivalent index in second and third array has the minimum distances to the point (0.5, 0.5) something like! is it clear?
Then perhaps you are looking for np.lexsort([np.abs(second-0.5), np.abs(third-0.5)]). If that is not what you want, please show the calculation you are using to obtain [0,1,2].
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.