我想排序(最短到最长)一个数组'a'(如下所示)到原点或点的距离(在我的情况下为0,0),并将其存储到类似的数组类型'b'或替换数组'a'
下面给出的点是三维numpy数组
[[[ 510. 11.]]
[[ 651. 276.]]
[[ 269. 70.]]
[[ 920. 26.]]
[[ 513. 21.]]
[[ 1197. 620.]]
[[ 407. 268.]]
[[ 452. 35.]]
[[ 435. 3.]]
[[ 520. 20.]]
[[ 1151. 499.]]
[[ 104. 26.]]
[[ 754. 28.]]
[[ 263. 111.]]
[[ 731. 12.]]
[[ 972. 200.]]
[[ 1186. 614.]]
[[ 437. 2.]]
[[ 1096. 68.]]
[[ 997. 201.]]
[[ 1087. 200.]]
[[ 913. 201.]]
[[ 1156. 510.]]
[[ 994. 230.]]
[[ 793. 29.]]
[[ 514. 19.]]]
我找不到任何有用的信息,关于这类三维np数组排序
ps:这些点'a'是从Goodfeaturestotrack,OPEN CV,python 3.6中获得的
如何将数组清除为Null类型?
#this is clustering algorithm
for index in range(len(a): #a is the above matrix 3d np array
#find distance was already defined and is euclidean distance formula
if findDistance(a[index][0], a[index][1], a[index + 1][0], a[index + 1][1]) < 3: #calculation euclidean distance between ai and ai+1
c.append(index)
if findDistance(a[index][0], a[index][1], a[index + 1][0], a[index + 1][1]) > 3: #calculation euclidean distance between ai and ai+1
if len(c) > 10:
cp = np.insert(cp, c, 0)
c = [] # should clear c **is this correct ??**
3条答案
按热度按时间rkkpypqq1#
我喜欢用它来方便地计算几种数组格式的距离...它不是一行程序,但它很好用。关于它的实现细节可以在堆栈的其他地方找到,方法是搜索'einsum'和numpy作为关键字。需要将numpy作为np导入,这只是def,你需要2个数组
然后您可以根据需要对结果进行排序,例如
附录
我应该补充说,您可以使用argsort获得排序后的值,如下所示
wpx232ag2#
c7rzv4ha3#
你可以使用scikit-learn内置的pairwise-distance函数。