[R] lower.tri/upper.tri functions unable to make a matrix symmetric!

William Dunlap wdunlap at tibco.com
Wed Feb 2 00:20:26 CET 2011


Try replacing
    MyMatrix[lower.tri(MyMatrix)] <- MyMatrix[upper.tri(MyMatrix)]
with
    MyMatrix[lower.tri(MyMatrix)] <- t(MyMatrix[upper.tri(MyMatrix)])
so it puts the transponse of the upper triangle into the lower.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Ron Michael
> Sent: Tuesday, February 01, 2011 2:04 PM
> To: r-help at r-project.org
> Subject: [R] lower.tri/upper.tri functions unable to make a 
> matrix symmetric!
> 
> Dear all, I have having a strange problem with 
> upper.tri()/lower.tri() functions which I used to use to make 
> a matrix symmetric. However for a specific large matrix, they 
> seem not working. Initial I had following matrix:
> 
> > MyMatrix
>               [,1]         [,2]          [,3]          [,4]   
>        [,5]          [,6]          [,7]          [,8]         
>  [,9]         [,10]
>  [1,] 0.0002137831 1.791564e-06  1.308941e-06  1.320362e-06  
> 1.576466e-06 -2.090668e-06 -4.480179e-08  1.617517e-06  
> 1.188307e-06 -6.592009e-06
>  [2,]           NA 2.313636e-04 -2.028031e-06 -2.749156e-06 
> -4.670093e-07  2.630111e-06  1.364227e-06 -5.065928e-06  
> 9.069602e-07  7.034820e-07
>  [3,]           NA           NA  2.800611e-04  2.530742e-06  
> 1.237191e-06  6.105895e-07 -2.095904e-06 -5.090979e-06  
> 1.180013e-06  8.172282e-06
>  [4,]           NA           NA            NA  3.219580e-04 
> -8.626130e-08  2.877486e-06 -4.387613e-06  1.551715e-06  
> 5.999397e-07 -4.047684e-06
>  [5,]           NA           NA            NA            NA  
> 2.343739e-04  5.086490e-06  2.839762e-06 -5.081967e-07  
> 1.121733e-07 -3.601529e-06
>  [6,]           NA           NA            NA            NA   
>          NA  2.488756e-04  2.118398e-07 -1.879584e-06  
> 7.186348e-07 -3.602057e-06
>  [7,]           NA           NA            NA            NA   
>          NA            NA  2.946481e-04  2.454685e-06 
> -2.310217e-07  2.787164e-06
>  [8,]           NA           NA            NA            NA   
>          NA            NA            NA  1.899915e-04  
> 1.224963e-06 -1.533257e-07
>  [9,]           NA           NA            NA            NA   
>          NA            NA            NA            NA  
> 2.009841e-04 -6.389595e-07
> [10,]           NA           NA            NA            NA   
>          NA            NA            NA            NA         
>    NA  4.427314e-04
> [11,]           NA           NA            NA            NA   
>          NA            NA            NA            NA         
>    NA            NA
> [12,]           NA           NA            NA            NA   
>          NA            NA            NA            NA         
>    NA            NA
> [13,]           NA           NA            NA            NA   
>          NA            NA            NA            NA         
>    NA            NA
> [14,]           NA           NA            NA            NA   
>          NA            NA            NA            NA         
>    NA            NA
> [15,]           NA           NA            NA            NA   
>          NA            NA            NA            NA         
>    NA            NA
> [16,]           NA           NA            NA            NA   
>          NA            NA            NA            NA         
>    NA            NA
>               [,11]         [,12]         [,13]         [,14] 
>         [,15]         [,16]
>  [1,]  1.611978e-06  1.156455e-06  2.429236e-06 -6.371224e-07 
>  3.092417e-07  3.011528e-06
>  [2,] -5.510187e-06 -8.998582e-07  9.614807e-07 -8.311204e-07 
> -8.974948e-07 -3.812490e-08
>  [3,] -1.373343e-06 -5.213719e-07 -1.775968e-06  1.206253e-06 
>  6.585452e-07  1.272045e-06
>  [4,] -2.111128e-07  2.386934e-06  2.966653e-06  2.694338e-06 
>  2.838703e-06  3.419373e-06
>  [5,]  4.707156e-06  2.080091e-06 -1.449516e-06  2.091399e-07 
>  1.564799e-06  1.203982e-06
>  [6,] -7.174750e-06 -1.016559e-06  3.679732e-07 -1.604075e-06 
>  3.363196e-06 -2.106617e-06
>  [7,] -1.358783e-06 -2.298378e-06 -1.822507e-06  1.792765e-06 
>  1.201043e-06 -2.553821e-06
>  [8,]  5.935161e-06 -3.651090e-07  2.112346e-06 -1.432126e-06 
> -3.692184e-07 -2.214944e-06
>  [9,]  3.847311e-07 -1.156356e-07  4.155777e-06 -1.627045e-06 
>  1.789992e-07  3.229348e-06
> [10,]  3.123505e-06  3.504098e-06 -1.462336e-06  3.846515e-06 
> -2.693877e-08  3.698078e-06
> [11,]  3.421896e-04 -1.334050e-06 -4.764588e-06 -4.065790e-07 
>  1.312966e-06  1.531616e-06
> [12,]            NA  1.600832e-04  7.403796e-07  2.684925e-06 
>  2.003153e-06  1.217396e-06
> [13,]            NA            NA  3.573365e-04 -1.540675e-06 
>  4.166715e-07  1.457177e-06
> [14,]            NA            NA            NA  2.277771e-04 
>  1.856102e-06 -1.513366e-06
> [15,]            NA            NA            NA            NA 
>  2.065449e-04  1.294455e-06
> [16,]            NA            NA            NA            NA 
>            NA  2.056605e-04
> > MyMatrix[lower.tri(MyMatrix)] <- MyMatrix[upper.tri(MyMatrix)]
> > isSymmetric(MyMatrix)
> [1] FALSE
> 
> Can somebody please tell me what is the problem with 
> lower.tri/upper.tri functions? why they are not making my 
> matrix symmetric?
> 
> Thanks
> 
> 
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 



More information about the R-help mailing list