1
我有一個包含2列的矩陣,它對應於軌跡中點的座標(x,y)。我想用歐氏距離來計算軌跡的總長度。軌跡的總長度
首先我打開我的軌跡文件
fichier="fichier_position_2_test.txt"
file = open(fichier, "rb")
for ligne in file:
ligne = ligne.split(' ')
m.append(array([(ligne[1]),(ligne[2])]))
然後我計算矩陣
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
兩個點之間的歐氏距離但是它不工作這麼好
這裏我的檔案 http://s000.tinyupload.com/?file_id=26745790747175243934
下面是一些測試
#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)
而結果
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_'
我認爲問題來自矩陣m。因爲,看到用矩陣A(示例)它給出了預期的結果(0.0194054064971)