Skip to content

almost lower triangular matrices

3 messages · Michael Anyadike-Danes, Douglas Bates, Spencer Graves

#
Michael Anyadike-Danes wrote:
You can use scan to get the entries from the file in row order.  Then 
create the matrix to hold the result and overwrite the elements in the 
upper triangle with scanned vector.  (R stores matrices in column-major 
order so the row-major order from your file corresponds to the upper 
triangle, not the lower triangle).  I'll leave it to you to work out the 
  symmetrization operation.

 > file.show("/tmp/tri.dat")
1
2 3
4 5 6
7 8 9 10

 > mm <- array(0, c(4,4))
 > mm[upper.tri(mm, diag = TRUE)] <- scan("/tmp/tri.dat")
Read 10 items
 > mm
      [,1] [,2] [,3] [,4]
[1,]    1    2    4    7
[2,]    0    3    5    8
[3,]    0    0    6    9
[4,]    0    0    0   10
 > (res <- mm + t(mm))
      [,1] [,2] [,3] [,4]
[1,]    2    2    4    7
[2,]    2    6    5    8
[3,]    4    5   12    9
[4,]    7    8    9   20
 > diag(res) <- diag(res)/2
 > res
      [,1] [,2] [,3] [,4]
[1,]    1    2    4    7
[2,]    2    3    5    8
[3,]    4    5    6    9
[4,]    7    8    9   10
#
Output from which program?  If the output is of class "dist", then 
"as.matrix" should give you what you want: 

 > set.seed(1)
 > X <- array(rnorm(12), dim=c(4,3))
 > (dX <- dist(X))
          1         2         3
2 1.6598683                   
3 0.9720025 2.4600033         
4 2.2666735 2.2149274 2.6890545
 > length(dX)
[1] 6
 > as.matrix(dX)
          1        2         3        4
1 0.0000000 1.659868 0.9720025 2.266674
2 1.6598683 0.000000 2.4600033 2.214927
3 0.9720025 2.460003 0.0000000 2.689054
4 2.2666735 2.214927 2.6890545 0.000000
 >
      hope this helps. 
      spencer graves
Douglas Bates wrote: