【发布时间】:2015-06-22 08:06:08
【问题描述】:
我有一个包含 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)
【问题讨论】: