2017-07-20 67 views
0

我工作的連鎖不平衡和軟件,輸出類似如下: Figure current相應的矩陣結構

我真正想要的是矩陣對應的半以及1對這樣的對角線: Figure anticipated

我想知道這是否可以很容易地在R或Python中完成? 感謝您的幫助。

+0

哪種類型你得到矩陣?你想在Python或R中只使用algorythm來做到這一點與簡單的2維列表或代碼必須與whis表中斷然後獲得數據轉換爲列表並做algorythm? – Vladyslav

回答

0
> library(sem) 
> mat <- matrix(1:64, 8, 8) 
> mat[lower.tri(mat)] <- 0 
> 
> diag(mat) <- 1 
> mat 
    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] 
[1,] 1 9 17 25 33 41 49 57 
[2,] 0 1 18 26 34 42 50 58 
[3,] 0 0 1 27 35 43 51 59 
[4,] 0 0 0 1 36 44 52 60 
[5,] 0 0 0 0 1 45 53 61 
[6,] 0 0 0 0 0 1 54 62 
[7,] 0 0 0 0 0 0 1 63 
[8,] 0 0 0 0 0 0 0 1 
> mat[lower.tri(mat)] <- t(mat)[lower.tri(mat)] 
> mat 
    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] 
[1,] 1 9 17 25 33 41 49 57 
[2,] 9 1 18 26 34 42 50 58 
[3,] 17 18 1 27 35 43 51 59 
[4,] 25 26 27 1 36 44 52 60 
[5,] 33 34 35 36 1 45 53 61 
[6,] 41 42 43 44 45 1 54 62 
[7,] 49 50 51 52 53 54 1 63 
[8,] 57 58 59 60 61 62 63 1 
+0

感謝Guillaume!這工作! :) – David

+0

@大衛,你評論並接受了另一篇文章,不是我的! –

+0

@大衛或你與名字混淆! ? – RUser

0

你可以使用Python做到這一點,numpy的輕鬆:

import numpy as np 

# Create the empty matrix 
d = np.zeros((8,8)) 

# Create the upper triangular matrix 
d[0,2:]=1 
d[1,2]=0.839 
d[1,3]=1 
d[1,4:6]=0.736 
d[1,6:]=0.864 
d[2,3:]=1 
d[3,4:]=1 
d[4,5:]=1 
d[5,6:]=1 
d[4:6,7]=0.933 
d[6,7]=0.88 
print(d) 


# Create the full matrix with transpose and identity matrix 
dFUll = d + d.T + np.eye(8) 
print(dFull)