0

I have an array of 4 coordinates in each array

x0 = [1,2,3,4] #x_coordinates
y0 = [1,2,3,4] #y_coordinates

x1 = [11,12,13,14] #x_coordinates
y1 = [11,12,13,14] #y_coordinates

I would like to find the distance between the two coordinates.

distance = sqrt((x1 - x0)^2 + (y1 - y0)^2)

So, I tried

distance = math.sqrt((x1 - x0)**2 + (y1 - y0)**2)

But the error is TypeError: only length-1 arrays can be converted to Python scalars.

Isnt it possible to do an element wise operation by just using the array_variable? Or do I have to iterate it by using a for loop?

I found this as a probable answer, but looks quite complicated with numpy. calculating distance between two numpy arrays

EDIT:

Tried the following

x_dist = pow((x1 - x0), 2)
y_dist = pow((y1 - y0), 2)
dist = x_dist+y_dist
dist=dist**2
5
  • 1
    You cannot subtract lists in python. Consider using numpy arrays. Commented Mar 25, 2018 at 19:37
  • @DyZ : are you sure, that you cant subtract ists in python?? x1-x0 looks like a completely genuine statement. Commented Mar 25, 2018 at 19:41
  • Yes, I am sure. But are you sure that your x1 and x0 are lists? Commented Mar 25, 2018 at 19:45
  • @DyZ yes, it is.. print x0 gave me [ 413.59921265 412.74182129 411.94470215 411.37411499]. Please look at my edited answer and it gives no error anymore. so I am confused why is it working, when its not possible to subtract lists in python. Commented Mar 25, 2018 at 19:49
  • sorry its <type 'numpy.ndarray'> and not a list !! I got it now. Commented Mar 25, 2018 at 19:53

2 Answers 2

1

Yes, with plain python lists you have to use a loop or comprehension to do things element-wise.

It's not complicated with numpy, you just have to wrap each list in an array:

from numpy import array, sqrt

x0 = array([1, 2, 3, 4])  # x_coordinates
y0 = array([1, 2, 3, 4])  # y_coordinates

x1 = array([11, 12, 13, 14])  # x_coordinates
y1 = array([11, 12, 13, 14])  # y_coordinates

print(sqrt((x1 - x0) ** 2 + (y1 - y0) ** 2))

Here is how to do it in plain Python using a loop comprehension:

from math import sqrt

x0_list = [1, 2, 3, 4]  # x_coordinates
y0_list = [1, 2, 3, 4]  # y_coordinates

x1_list = [11, 12, 13, 14]  # x_coordinates
y1_list = [11, 12, 13, 14]  # y_coordinates

print([sqrt((x1 - x0) ** 2 + (y1 - y0) ** 2)
       for x0, y0, x1, y1 in zip(x0_list, y0_list, x1_list, y1_list)])
Sign up to request clarification or add additional context in comments.

6 Comments

can you please see my edit and tell me if thats another way to do it?
@infoclogged if you're using lists, no. If you're using something else, you tell me if it works.
how do I find the type of x0? Then i will tell you. But by printing x0 and x1, they do look like a list to me..
print(type(x0)). But how did you create them?
its <type 'numpy.ndarray'>... dint know that there were so many array types.. I come from the c++ world. sorry. thanks for your answer. it works.
|
0

With out numpy, this could be used:

import math

def func(x0,x1,y0,y1):

    distance = []

    for a,b,c,d in zip(x0,x1,y0,y1):
        result = math.sqrt((b - a)**2 + (d - c)**2)
        distance.append(result)

    return distance

x0 = [1,2,3,4] #x_coordinates
y0 = [1,2,3,4] #y_coordinates

x1 = [11,12,13,14] #x_coordinates
y1 = [11,12,13,14] #y_coordinates

print(func(x0, x1, y0, y1))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.