[R] Achieving 'reverse-Vech' of a matrix

(Ted Harding) ted.harding at wlandres.net
Sat Jul 23 19:43:08 CEST 2011


On 23-Jul-11 17:12:09, Megh Dal wrote:
> Let say i have a square matrix and applied the 'vech' operator to stack
> the lower triangular elements into a vector:
> 
>> Mat <- matrix(1:25, 5)
>> Mat
> _ _ _[,1] [,2] [,3] [,4] [,5]
> [1,] _ _1 _ _6 _ 11 _ 16 _ 21
> [2,] _ _2 _ _7 _ 12 _ 17 _ 22
> [3,] _ _3 _ _8 _ 13 _ 18 _ 23
> [4,] _ _4 _ _9 _ 14 _ 19 _ 24
> [5,] _ _5 _ 10 _ 15 _ 20 _ 25
>> Mat[lower.tri(Mat)]
> _[1] _2 _3 _4 _5 _8 _9 10 14 15 20
> 
> 
> Now, I want to reverse-work with the resulting matrix. Means, given a
> "correct" vector, I want to place the elements of this vector into the
> lower-triangular portion of some "correct" square matrix.
> 
> Would somebody help me to implement that?
> Thanks,

If you are already using 'vech()' in the MCMCpack package to extract
the lower-triangular vector, then that package contains also the
function 'xpnd()' which reconstructs a symmetric matric from the
Vech.

Thus, is you have a matrix A whose lower triangular part you
want to replace as you describe, and a vector X which is the Vech
of some matrix, then

  B <- xpnd(X)
  A[lower.tri(A)] <- B[lower.tri(B)]

should do it (not tested by me, however ... ).

Hoping this helps,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.harding at wlandres.net>
Fax-to-email: +44 (0)870 094 0861
Date: 23-Jul-11                                       Time: 18:43:05
------------------------------ XFMail ------------------------------



More information about the R-help mailing list