Question

I think my code could be done better ...

What I want:

array a = ([1,2,3],[4,5,6],[7,8,9])
array b = ([2,3,4],[2,2,2])

Euclidean distance from a[0] to b[0] and b[1] and then the minimum of that and so on with the other values...

Result should be something like that

result = ([1,2]) 

I think the way I did it is a little bit complicated:

result = [0]*len(b)
for i in a:
    c = 0
    minimum = euclid(a[0],b[0])
    place = 0
    for j in b:
        c=c+1
    if (minimum > euclid(i,j)):
          minimum = euclid(i,j)
          place = c 
    result[place-1] = result[place-1]+1

ok well i try to explain it better. I have got two array A and B Array A have got 3 Values (Value <=> [1,2,3]) Now i want to calculate the euclidean distance of All Values of A with all values of B and Count how often B[0] or B[1] was the minimum.

so i start the code manual: At first i calculate a[0] with b[0] and found out that thats the minimum because there is no minimum at the moment. At next i calculate a[0] with b[1] and found out that euclidean(a[0],b[1]) < euchlidean(a[0],b[0]) so i set the array wich is at the moment at c([0,0]) to c([0,1]). Next i calculate the euclidean distance a[1] to b[0] and b[1] and found out that b[0] ist the minimum of this two values so i set c to c([1,1])...

Était-ce utile?

La solution

I believe what you are saying is that you have two lists of points - a & b. For each point in a, increment a counter for whichever point in b is the closest (minimum euclidean distance). Is that correct?

Here's how I would achieve that:

results = [0] * len(b)
for p_a in a:
    dists = [euclid(p_a, p_b) for p_b in b]
    min_index = dists.index(min(dists))
    results[min_index] += 1
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top