Condensed matrix function to find pairs

后端 未结 7 1145
忘了有多久
忘了有多久 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:58

    This is in addition to the answer provided by phynfo and your comment. It does not feel like a clean design to me to infer the dimension of the matrix from the length of the compressed matrix. That said, here is how you can compute it:

    from math import sqrt, ceil
    
    for i in range(1,10):
       thelen = (i * (i+1)) / 2
       thedim = sqrt(2*thelen + ceil(sqrt(2*thelen)))
       print "compressed array of length %d has dimension %d" % (thelen, thedim)
    

    The argument to the outer square root should always be a square integer, but sqrt returns a floating point number, so some care is needed when using this.

    0 讨论(0)
提交回复
热议问题