简明矩阵功能找到对的 [英] Condensed matrix function to find pairs
问题描述
有关的一组观察:
[a1,a2,a3,a4,a5]
他们的成对距离
their pairwise distances
d=[[0,a12,a13,a14,a15]
[a21,0,a23,a24,a25]
[a31,a32,0,a34,a35]
[a41,a42,a43,0,a45]
[a51,a52,a53,a54,0]]
在一个浓缩的矩阵形式给定(以上的上三角,从计算的 scipy.spatial.distance.pdist
):
c=[a12,a13,a14,a15,a23,a24,a25,a34,a35,a45]
现在的问题是,因为我有指数在凝聚基体有一个函数(在Python preferably)的 F 即可快速给这两个观察来计算呢?
The question is, given that I have the index in the condensed matrix is there a function (in python preferably) f to quickly give which two observations were used to calculate them?
f(c,0)=(1,2)
f(c,5)=(2,4)
f(c,9)=(4,5)
...
我已经尝试了一些解决方案,但没有任何值得一提的:(
I have tried some solutions but none worth mentioning :(
推荐答案
您可能会发现<一href="http://docs.scipy.org/doc/numpy-1.5.x/reference/generated/numpy.triu_indices.html#numpy.triu_indices"相对=nofollow> triu_indices 的有用。像,
You may find triu_indices useful. Like,
In []: ti= triu_indices(5, 1)
In []: r, c= ti[0][5], ti[1][5]
In []: r, c
Out[]: (1, 3)
只需注意指数从0开始,你喜欢,你可以调整它,例如:
Just notice that indices starts from 0. You may adjust it as you like, for example:
In []: def f(n, c):
..: n= ceil(sqrt(2* n))
..: ti= triu_indices(n, 1)
..: return ti[0][c]+ 1, ti[1][c]+ 1
..:
In []: f(len(c), 5)
Out[]: (2, 4)
这篇关于简明矩阵功能找到对的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!