[R] Package corpcor: Putting symmetric matrix entries in vector
Steven Yen
syen04 at gmail.com
Sat Jan 31 01:34:49 CET 2015
Great! Thanks. Thanks to all who tried to help.
as.vector(r[upper.tri(r)]) does it:
> e<-as.matrix(cbind(u1,u2,u3,v1,v2,v3))
> r<-cor(e); r
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1.00000000 0.5240809 0.47996616 0.11200672 -0.1751103 -0.09276455
[2,] 0.52408090 1.0000000 0.54135982 -0.15985028 -0.2627738 -0.14184545
[3,] 0.47996616 0.5413598 1.00000000 -0.06823105 -0.2046897 -0.23815967
[4,] 0.11200672 -0.1598503 -0.06823105 1.00000000 0.2211311 0.08977677
[5,] -0.17511026 -0.2627738 -0.20468966 0.22113112 1.0000000 0.23567235
[6,] -0.09276455 -0.1418455 -0.23815967 0.08977677 0.2356724 1.00000000
> as.vector(r[upper.tri(r)])
[1] 0.52408090 0.47996616 0.54135982 0.11200672 -0.15985028 -0.06823105
[7] -0.17511026 -0.26277383 -0.20468966 0.22113112 -0.09276455 -0.14184545
[13] -0.23815967 0.08977677 0.23567235
At 06:56 PM 1/30/2015, Peter Langfelder wrote:
>If you have a symmetric matrix, you can work with the upper triangle
>instead of the lower one, and you get what you want by simply using
>
>as.vector(A[upper.tri(A)])
>
>Example:
>
> > a = matrix(rnorm(16), 4, 4)
> > A = a + t(a)
> > A
> [,1] [,2] [,3] [,4]
>[1,] 0.3341294 0.5460334 -0.4388050 1.09415343
>[2,] 0.5460334 0.1595501 0.3907721 0.24021833
>[3,] -0.4388050 0.3907721 -0.4024922 -1.62140865
>[4,] 1.0941534 0.2402183 -1.6214086 0.03987924
> > as.vector(A[upper.tri(A)])
>[1] 0.5460334 -0.4388050 0.3907721 1.0941534 0.2402183 -1.6214086
>
>No need to play with potentially error-prone index vectors; upper.tri
>does that for you.
>
>Hope this helps,
>
>Peter
>
>On Fri, Jan 30, 2015 at 3:03 PM, Steven Yen <syen04 at gmail.com> wrote:
> > Dear
> > I use sm2vec from package corpcor to puts the lower triagonal entries of a
> > symmetric matrix (matrix A) into a vector. However, sm2vec goes downward
> > (columnwise, vector B), but I would like it to go across (rowwise). So I
> > define a vector to re-map the vector (vector C). This works. But is there a
> > short-cut (simpler way)? Thank you.
> >
> >> A<-cor(e); A
> > [,1] [,2] [,3] [,4] [,5] [,6]
> > [1,] 1.00000000 0.5240809 0.47996616 0.11200672 -0.1751103 -0.09276455
> > [2,] 0.52408090 1.0000000 0.54135982 -0.15985028 -0.2627738 -0.14184545
> > [3,] 0.47996616 0.5413598 1.00000000 -0.06823105 -0.2046897 -0.23815967
> > [4,] 0.11200672 -0.1598503 -0.06823105 1.00000000 0.2211311 0.08977677
> > [5,] -0.17511026 -0.2627738 -0.20468966 0.22113112 1.0000000 0.23567235
> > [6,] -0.09276455 -0.1418455 -0.23815967 0.08977677 0.2356724 1.00000000
> >> B<-sm2vec(A); B
> > [1] 0.52408090 0.47996616 0.11200672 -0.17511026 -0.09276455
> > [6] 0.54135982 -0.15985028 -0.26277383 -0.14184545 -0.06823105
> > [11] -0.20468966 -0.23815967 0.22113112 0.08977677 0.23567235
> >> jj<-c(1,2,6,3,7,10,4,8,11,13,5,9,12,14,15)
> >> C<-B[jj]; C
> > [1] 0.52408090 0.47996616 0.54135982 0.11200672 -0.15985028
> > [6] -0.06823105 -0.17511026 -0.26277383 -0.20468966 0.22113112
> > [11] -0.09276455 -0.14184545 -0.23815967 0.08977677 0.23567235
> >
> > ______________________________________________
> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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