[R] Help creating a symmetric matrix?
Rui Barradas
ruipbarradas at sapo.pt
Sat Dec 24 03:31:37 CET 2011
Matt Considine wrote
>
> Hi,
> I am trying to work with the output of the MINE analysis routine found at
> http://www.exploredata.net
>
> Specifically, I am trying to read the results into a matrix (ideally an
> n x n x 6 matrix, but I'll settle right now for getting one column into
> a matrix.)
>
> The problem I have is not knowing how to take what amounts to being one
> half of a symmetric matrix - excluding the diagonal - and getting it
> into a matrix. I have tried using "lower.tri" as found here
> https://stat.ethz.ch/pipermail/r-help/2008-September/174516.html
> but it appears to only partially fill in the matrix. My code and an
> example of the output is below. Can anyone point me to an example that
> shows how to create a matrix with this sort of input?
>
> Thank you in advance,
> Matt
>
> #v<-newx[,3]
> #or, for the sake of this example
> v<-c(0.33740, 0.26657, 0.23388, 0.23122, 0.21476, 0.20829, 0.20486,
> 0.19439, 0.19237,
> 0.18633, 0.17298, 0.17174, 0.16822, 0.16480, 0.15027)
> z<-diag(6)
> ind <- lower.tri(z)
> z[ind] <- t(v)[ind]
>
> z
> [,1] [,2] [,3] [,4] [,5] [,6]
> [1,] 1.00000 0.00000 0 0 0 0
> [2,] 0.26657 1.00000 0 0 0 0
> [3,] 0.23388 0.19237 1 0 0 0
> [4,] 0.23122 0.18633 NA 1 0 0
> [5,] 0.21476 0.17298 NA NA 1 0
> [6,] 0.20829 0.17174 NA NA NA 1
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@ 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.
>
Hello,
Aren't you complicating?
In the last line of your code, why use 'v[ind]' if 'ind' indexes the matrix,
not the vector?
z<-diag(6)
ind <- lower.tri(z)
z[ind] <- v #This works
z
Rui Barradas
--
View this message in context: http://r.789695.n4.nabble.com/Help-creating-a-symmetric-matrix-tp4227301p4230335.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list