vechindex {lazy.mat}R Documentation

Returns the index to convert vech(Mat) to Mat and vice versa.

Description

Returns the index to convert vech(Mat) to Mat and vice versa.

Usage

vechindex(n, nodiag = 0, byrow = 1, lowerh = 1, type = 1, i = NULL, j = NULL)

Arguments

n

# of rows and columns

nodiag

= 1 to omit the diagonal elements

byrow

= 1

lowerh

= 1

type

= 1 to generate vector index
= 2 to generate matrix index.

i

The row index

j

The col index

Details

Let A be n x n. To create vech(A) from A, use
index <- vecvecindex( nrow(A) ); vechA <- A[index]
which is equivalent to: vechA=vech(A).
To see what elements are included in the lower triangle part of the matrix, use
index <- vecvecindex( nrow(A), type=2 ) .
This will generate the combination of subscripts (i,j) in a matrix.
To recover A from vechA, use
index <- vechindex( n ); A[index] <- vechA
which is equivalent to: A <- vechinv(vechA).

When i and j, i >= j, parameters are given, the location of the A[i,j] in vech(A) will be returned in a vector of the same size as i and j.



Can be used with character matrix.

Value

vector or matrix of index

Examples


# simlulating vech using vechindex
ndim=4
M=demomat(ndim,ndim,root="m")
vechM=vech(M); vechM2=M[vechindex(ndim)]
Print( vechM, vechM2 )
vechM=vech(M, byrow=0); vechM2=M[vechindex(ndim, byrow=0)]
Print( vechM, vechM2 )
vechM=vech(M, lowerh=0); vechM2=M[vechindex(ndim, lowerh=0)]
Print( vechM, vechM2 )
vechM=vech(M, byrow=0, lowerh=0); vechM2=M[vechindex(ndim, byrow=0, lowerh=0)]
Print( vechM, vechM2 )

# simulating vechinv using vechindex
M2=matrix(NA,ndim,ndim)
M2[vechindex(ndim)]=vech(M)
Print(M2)
M2=matrix(NA,ndim,ndim)
M2[vechindex(ndim, lowerh=0)]=vech(M,lowerh=0)
Print(M2)

# i and j from vector index k
n <- 5
ij <- vechindex(n,type=2)
# k from i and j
k <- vechindex(n, i=ij[,1], j=ij[,2])
Print(ij,k)


[Package lazy.mat version 0.1.4 Index]