[R] Package corpcor: Putting symmetric matrix entries in vector

David Winsemius dwinsemius at comcast.net
Sat Jan 31 00:43:56 CET 2015


On Jan 30, 2015, at 3:03 PM, Steven Yen 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.

What about using this sequence to instead extract from the original A-Matrix:

c(2, unlist( sapply( 3:6, function(n) c( n, n+6*seq(n-2) ) )) )
 [1]  2  3  9  4 10 16  5 11 17 23  6 12 18 24 30

> idx <- c(2, unlist( sapply( 3:6, function(n) c( n, n+6*seq(n-2) ) )) )
> A[idx]
 [1]  0.52408090  0.47996616  0.54135982  0.11200672 -0.15985028 -0.06823105
 [7] -0.17511030 -0.26277380 -0.20468970  0.22113110 -0.09276455 -0.14184545
[13] -0.23815967  0.08977677  0.23567235

> 
> > 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.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list