Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在研究 R 中的一个循环:
dypol和wnc是 1×3 矩阵并且x是 1×100 矩阵。我希望循环返回一个 3×100 矩阵(每列累积)。我有这个:
dypol
wnc
x
For (i in 1:100) { i=dypol*t(x^2)-dypol+wnc {yi = cumsum(i) } }
但它只返回第一行。
试试这个。我已将循环转换为sapply调用(有效循环x),然后应用于cumsum结果矩阵的列:
sapply
cumsum
x <- 1:100 wnc <- c(0.123, 0.263, 0.223) dypol <- c(.05, .30, .02) Z <- t(sapply(x, function(x)dypol * x ^ 2 - dypol + wnc)) apply(Z, 2, cumsum)