下三角行列に要素を代入するとき,R では
n = 4
mat = matrix(0, n, n)
# A matrix: 4 × 4 of type dbl
# 0 0 0 0
# 0 0 0 0
# 0 0 0 0
# 0 0 0 0
x = c(0.1, 0.3, 0.6, 1.2, 3.1, 5.7)
mat[lower.tri(mat, diag=FALSE)] <- x
mat
# A matrix: 4 × 4 of type dbl
# 0.0 0.0 0.0 0
# 0.1 0.0 0.0 0
# 0.3 1.2 0.0 0
# 0.6 3.1 5.7 0
とするのが定道。
Julia では lower.tri のような関数はない(と思う)。
以下のようにするしかないか。
n = 4
mat = zeros(n, n)
# 4×4 Array{Float64,2}:
# 0.0 0.0 0.0 0.0
# 0.0 0.0 0.0 0.0
# 0.0 0.0 0.0 0.0
# 0.0 0.0 0.0 0.0
x = [0.1, 0.3, 0.6, 1.2, 3.1, 5.7] # length(x) == (n * (n - 1) ÷ 2)
print(collect(x)) # [0.1, 0.3, 0.6, 1.2, 3.1, 5.7]
lowertri = [i > j for i = 1:n, j = 1:n]
# 4×4 Array{Bool,2}:
# 0 0 0 0
# 1 0 0 0
# 1 1 0 0
# 1 1 1 0
mat[lowertri] = x;
mat
# 4×4 Array{Float64,2}:
# 0.0 0.0 0.0 0.0
# 0.1 0.0 0.0 0.0
# 0.3 1.2 0.0 0.0
# 0.6 3.1 5.7 0.0
lowertri = [i > j for i = 1:n, j = 1:n]
のように中間変数に代入しないようにすると
mat[[i > j for i = 1:n, j = 1:n]] = x
のようにも書けるから,[i > j for i = 1:n, j = 1:n] が lower.tri() に相当している。