I have a matrix containing 2 columns which correspond to the coordinates (x,y) of points in a trajectory. I want to calculate the total length of the trajectory using euclidean distance.
First I open my trajectory file
fichier="fichier_position_2_test.txt"
file = open(fichier, "rb")
for ligne in file:
ligne = ligne.split(' ')
m.append(array([(ligne[1]),(ligne[2])]))
Then I calculate the euclidean distance between two points in the matrix
def T(mat):
n=len(mat)
M=[0 for x in range(mat)]
for j in range(0,n-1):
val = sqrt((mat[j+1][0]-mat[j][0])*(mat[j+1][0]-mat[j][0]) + (mat[j+1][1]-mat[j][1])*(mat[j+1][1]-mat[j][1]))
M.append(val)
L = sum(M)
return L
However it doesn't work so well
Here is my file http://s000.tinyupload.com/?file_id=26745790747175243934
Here are some test
#TEST
A = array([(-4e-9,7.2e-6),(-5.7e-4,3.7e-4),(-8.7e-3,5.7e-3),(-1.2e-3,7.1e-4)])
print T(A)
print T(m)
And the result
0.0194054064971
Traceback (most recent call last):
File "tortuosity.py", line 46, in <module>
print T(m)
File "tortuosity.py", line 37, in T
val = sqrt((mat[j+1][0]-mat[j][0])*(mat[j+1][0]-mat[j][0]) + (mat[j+1][1]-mat[j][1])*(mat[j+1][1]-mat[j][1]))
TypeError: unsupported operand type(s) for -: 'numpy.string_' and 'numpy.string_'
I think the problem comes from the matrix m. Because, see that with the matrix A (an example) it gives the expected result (0.0194054064971)
The problem seems to be that you are never converting the strings from the file to floating point numbers. Thus your array contains a lot of strings (that look like numbers, but are still strings), and you can not take the difference of strings.
The minimum to make your code work is to change this line to:
But note that there are some other, minor issues:
0
, but then you append to those existing zeros, instead of overwriting them**
instead of repeating the same expression twicezip(list, list[1:])
to iterate pairs of consecutive lineswith
to open files, and do not usefile
as a variable name, as it's shadowing the typeAll in all, you could make your code quite a bit more compact:
Result for
m
is0.00132038721131
, and forA
is0.0194054064971
, as expected.