arrays - how to print for loop results in a upper triangular matrix in R -


the script in r follow.

   (i in 1:(ncol(k)-1)) #ncol=7,k dataset      (j in i:ncol(k)){      print(mi.empirical(rbind(k[, i],k[,j])))      } 

output given

[1] 0  [1] 0.1412579  [1] 0.4597332  [1] 0.382798  [1] 0.1162086  [1] 0.3379114  [1] 0.4848073  [1] 0  [1] 0.3103481  [1] 0.1556235  [1] 0.02243661  [1] 0.123117  [1] 0.2290079  [1] 0  [1] 0.1460452  [1] 0.3638203  [1] 0.2569532  [1] 0.1117262  [1] 0  [1] 0.1981758  [1] 0.05325737  [1] 0.03590259  [1] 0  [1] 0.1380921  [1] 0.3007399  [1] 0  [1] 0.1032283 

can tell me how print results in upper triangular matrix (7 x 7 matrix)? zeros (0) in diagonal. please me.

thank u in advance.

if want print results matrix, then, using dummy data

n <- 7 vec <- seq_len((0.5 * (n * (n-1)) + n)) ## dummy data, *inc* diagonal 

which play role of cumulated output mi.empirical(), create empty matrix

mat <- matrix(ncol = 7, nrow = 7) 

then index upper triangle of mat using upper.tri(mat, diag = true) , assign cumulated results it

mat[upper.tri(mat, diag = true)] <- vec  > mat      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,]    1    2    4    7   11   16   22 [2,]   na    3    5    8   12   17   23 [3,]   na   na    6    9   13   18   24 [4,]   na   na   na   10   14   19   25 [5,]   na   na   na   na   15   20   26 [6,]   na   na   na   na   na   21   27 [7,]   na   na   na   na   na   na   28 

you going need put outputs mi.empirical() single vector though, rather printing each intermediary result.


Comments