matindex_JPH {lazy.symbolic}R Documentation

vec(mat) を vec(t(mat)) に変換するためのインデックスを返す関数

Description

英語のヘルプファイルは: matindex

Arguments

nr

行数

nc

列数

triangle

= 1 三角部分を展開する場合

nodiag

= 1 triangle = 1 の場合、対角要素を除く

byrow

= 1 行優先で展開

lowerh

= 0 下三角部分(下半分)を展開

type

= 1 ベクトルインデックスを出力

print

= 1 結果を表示

Details

M を行列とし、v=vec(M) または v=vech(M) とする。
type = 2 の場合、出力されるインデックス idxnrow(M) x 2 行列で、v[k]M[idx[k,1], idx[k,2]] に対応する。
type = 1 の場合、出力されるインデックス idx はベクトルで、 v[k]M[idx[k]] に対応する。

いずれの場合も、ベクトル化は以下のように行える:

  v <- M[idx]

元の行列に戻す場合:

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

vec(M) を vec(t(M)) に変換する場合:

  # 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)

(これは旧関数 vecindex(現 vecvecindex)と同等)

また、旧関数 vec, vech, vechinvmatindex のインデックスを使って 以下のようにシミュレートできる:

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

(旧関数はインデックスに名前を付与して出力する機能あり)

Value

type に応じてベクトルまたは行列のインデックスを返す

Examples

# 正方行列
nr <- 4; nc <- 4
idx <- matindex(nr, nc, print=1)
idx <- matindex(nr, nc, byrow=1, print=1)

# 下三角部分: 列優先(vechindex byrow=0 と同じ)
idx <- matindex(nr, nc, tri=1, print=1)
idx <- matindex(nr, nc, tri=1, nodiag=1, print=1)
# 上三角部分: 列優先(vechindex 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)

# 下三角部分: 行優先(vechindex と同じ)
idx <- matindex(nr, nc, tri=1, byrow=1, print=1)
idx <- matindex(nr, nc, tri=1, nodiag=1, byrow=1, print=1)
# 上三角部分: 行優先(vechindex 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)

# 長方行列
nr <- 3; nc <- 4
idx <- matindex(nr, nc, print=1)
idx <- matindex(nr, nc, byrow=1, print=1)
idx <- matindex(nr, nc, tri=1, print=1)
idx <- matindex(nr, nc, tri=1, nodiag=1, print=1)
idx <- matindex(nr, nc, tri=1, lowerh=0, print=1)
idx <- matindex(nr, nc, tri=1, lowerh=0, nodiag=1, print=1)
idx <- matindex(nr, nc, tri=1, byrow=1, print=1)
idx <- matindex(nr, nc, tri=1, nodiag=1, byrow=1, print=1)
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 と type=2 のインデックス
idx <- matindex(nr, nc, tri=1, print=1)
idx <- matindex(nr, nc, tri=1, print=1, type=1)
Print(vechindex(nr, byrow=0))

# 行列のベクトル化と復元
nr <- 3; nc <- 3
M <- demomat(nr, nc)
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)))

# vec(M) と vec(t(M)) の変換(vecvecindex と同等)
nr <- 3; nc <- 4
M <- demomat(nr, nc)
idx1 <- matindex(nr, nc, byrow=1, type=1)
vectM <- vec(M)[idx1]
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.symbolic version 1.0.0.20250830 ]