【发布时间】:2020-01-02 13:46:31
【问题描述】:
我正在尝试为 n 行和 n+1 列创建以下矩阵 A。 n 可能在 20 或 30 左右,但出于问题的目的,我将其放在 4 和 5 之间。
这是我目前所拥有的:
N <- 5 # n+1
n <- 4 # n
columns <- list()
# first column:
columns[1] <- c(-1, 1, rep(0, N-2))
# all other columns:
for(i in N:2) {
columns[i] <- c((rep(0, N-i), 1, -2, 1, rep(0, i-3)))
}
# combine into matrix:
A <- cbind(columns)
我不断收到以下错误消息:
In columns[1] <- c(-1, 1, rep(0, N - 2)) :
number of items to replace is not a multiple of replacement length
后来
"for(i in N:2) {
columns[i] <- c((rep(0, N-i),"
}
Error: unexpected '}' in "}"
【问题讨论】:
-
您是否有理由为此特别需要一个 for 循环?
-
不,不一定是 for 循环,而是自动化的东西,我可以指定 n 并创建矩阵。接受其他想法...