Condensed matrix function to find pairs

后端 未结 7 1169
忘了有多久
忘了有多久 2020-12-24 05:18

For a set of observations:

[a1,a2,a3,a4,a5]

their pairwise distances

d=[[0,a12,a13,a14,a15]
   [a21,0,a23,a24,a25]
   [a31,         


        
7条回答
  •  渐次进展
    2020-12-24 05:57

    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)
    

    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)
    

提交回复
热议问题