Copy upper triangle to lower triangle in a python matrix

后端 未结 5 1729
耶瑟儿~
耶瑟儿~ 2020-12-24 01:31
       iluropoda_melanoleuca  bos_taurus  callithrix_jacchus  canis_familiaris
ailuropoda_melanoleuca     0        84.6                97.4                44
bos_tau         


        
5条回答
  •  庸人自扰
    2020-12-24 01:59

    Heres a better one i guess :

    >>> a = np.arange(16).reshape(4, 4)
    >>> print(a)
    array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11],
           [12, 13, 14, 15]])
    
    >>> iu = np.triu_indices(4,1)
    >>> il = (iu[1],iu[0])
    >>> a[il]=a[iu]
    >>> a
        array([[ 0,  1,  2,  3],
               [ 1,  5,  6,  7],
               [ 2,  6, 10, 11],
               [ 3,  7, 11, 15]])
    

提交回复
热议问题