matindex {lazy.mat}R Documentation

Returns the Index to Convert vec(mat) to vec(t(mat)), etc.

Description

Returns the Index to Convert vec(mat) to vec(t(mat)), etc.

Usage

matindex(
  nr,
  nc,
  triangle = 0,
  nodiag = 0,
  byrow = 0,
  lowerh = 1,
  type = 2,
  print = 0
)

Arguments

nr

# of rows.

nc

# of columns.

triangle

= 1 to roll-out the triangular part.

nodiag

= 1 to omit the diagonal elements when triangle = 1.

byrow

= 1 to roll-out by row.

lowerh

= 0 to roll-out the lower half (lower triangular) part.

type

= 1 to output vector index.

print

= 1 to print the result.

Details

Let M be a matrix and v=vec(M) or v=vech(M).
When type = 2, the resulting index, idx, is a nrow(M) x 2 matrix indicating that the v[k] is from M[idx[k,1],idx[k,2]].
When type = 1, the resulting index, idx, is a vector of length nrow(M) indicating that the v[k] is from M[idx[k]].

In either case, the vectorization can be done as

  v <- M[idx]
  

and the original matrix can be recovered as

  M2 <- matrix(NA,nrow(M),ncol(M))
  M2[idx] <- v
  


To convert vec(M) to vec(t(M)), use

  # vec(t(M)) from vec(M)
  idx <- matindex(nrow(M), ncol(M), byrow=1, type=1)
  vectM <- vec(M)[idx]
  # vec(M) from vec(t(M))
  idx <- matindex(ncol(M), nrow(M), byrow=1, type=1)
  vecM <- vec(t(M))[idx]
  Print(vec(M),vecM, vec(t(M)), vectM)
 

(Above is equivalent to old vecindex now renamed as vecvecindex.)
See the examples below and lazy.mat::Kmat function.

Old functions such as vec, vech, vechinv can be simulated by using the index from matindex as follows.

 vechindex( M, nodiag=, byrow=, lowerh= ) --->
   idx <- matindex( nrow(M), ncol(M), tri=1, nodiag=, byrow=, lowerh= )

(Old functions attach nice names to the output indices, though.)
See the examples below.

Value

A vector or matrix of indices depending on the type option.

Examples

# square matrix
nr <- 4; nc <- 4

# entire part
idx <- matindex( nr, nc, print=1 )
idx <- matindex( nr, nc, byrow=1, print=1 )

# lower triangular part by column: same as  vechindex  with byrow=0
idx <- matindex( nr, nc, tri=1, print=1 )
idx <- matindex( nr, nc, tri=1, nodiag=1, print=1 )
# upper triangular part: by column: same as vechindex  with byrow=0, lowerh=0
idx <- matindex( nr, nc, tri=1, lowerh=0, print=1 )
idx <- matindex( nr, nc, tri=1, lowerh=0, nodiag=1, print=1 )

# lower triangular part by row: same as vechindex
idx <- matindex( nr, nc, tri=1, byrow=1, print=1 )
idx <- matindex( nr, nc, tri=1, nodiag=1, byrow=1, print=1 )
# upper triangular part by row:same as  vechindex with lowerh=0
idx <- matindex( nr, nc, tri=1, lowerh=0, byrow=1, print=1 )
idx <- matindex( nr, nc, tri=1, lowerh=0, nodiag=1, byrow=1, print=1 )


# rectangular matrix
nr=3; nc=4

# entire part
idx <- matindex( nr, nc, print=1 )
idx <- matindex( nr, nc, byrow=1, print=1 )

# lower triangular part: by column
idx <- matindex( nr, nc, tri=1, print=1 )
idx <- matindex( nr, nc, tri=1, nodiag=1, print=1 )
# upper triangular part: by column
idx <- matindex( nr, nc, tri=1, lowerh=0, print=1 )
idx <- matindex( nr, nc, tri=1, lowerh=0, nodiag=1, print=1 )

# lower triangular part: by row
idx <- matindex( nr, nc, tri=1, byrow=1, print=1 )
idx <- matindex( nr, nc, tri=1, nodiag=1, byrow=1, print=1 )
# upper triangular part: by row
idx <- matindex( nr, nc, tri=1, lowerh=0, byrow=1, print=1 )
idx <- matindex( nr, nc, tri=1, lowerh=0, nodiag=1, byrow=1, print=1 )



# type=1 and type=2 index
idx <- matindex( nr, nc, tri=1, print=1 )
idx <- matindex( nr, nc, tri=1, print=1, type=1 )
Print( vechindex( nr, byrow=0 ) )



# vectorization and recovery of a matrix
nr <- 3; nc <- 3
M <- demomat(nr,nc)

# using type=2 index
idx <- matindex( nr, nc, tri=1, lowerh=1, byrow=0 )
v <- M[idx]
M2 <- matrix(NA,nrow(M),ncol(M))
M2[idx] <- v
Print(idx,v)
Print(M,M2)
Print(vech(M,byrow=0), vechinv(vech(M,byrow=0)))

# using type=1 index
idx <- matindex( nr, nc, tri=1, lowerh=1, byrow=0, type=1 )
v <- M[idx]
M2 <- matrix(NA,nrow(M),ncol(M))
M2[idx] <- v
Print(idx,v)
Print(M,M2)
Print(vech(M,byrow=0), vechinv(vech(M,byrow=0)))



# conversion between vec(M) and vec(t(M)): same as  vecvecindex
nr=3; nc=4
M <- demomat(nr,nc)
# vec(t(M)) from vec(M)
idx1 <- matindex(nr,nc, byrow=1, type=1)
vectM <- vec(M)[idx1]
# vec(M) from vec(t(M))
idx2 <- matindex(nc,nr, byrow=1, type=1)
vecM <- vec(t(M))[idx2]
Print(vec(M),vecM, vec(t(M)), vectM)
Print(idx1,vecvecindex(nr,nc),idx2,vecvecindex(nc,nr))


[Package lazy.mat version 0.1.4 Index]