如何使用“for loop”来计算p(2)p(3)p(4)p(5),然后同时打印它们?

时间:2017-10-23 15:40:04

标签: r

如何使用循环计算p(2)p(3)p(4)p(5),然后同时打印它们?

A<-matrix(c(0.75,0.25,0,0.25,0,0.75,0,0.75,0.25) , nrow = 3)
p0=t(matrix(c(0,0,1) , nrow = 1))
p1=t(A) %*% p0

问题:

p2=t(A) % * % p1,p3=t(A) % * % p2,p4=t(A) % * % p3,p5=t(A) % * % p4

1 个答案:

答案 0 :(得分:0)

这是一个想法:

A <- matrix(c(0.75,0.25,0,0.25,0,0.75,0,0.75,0.25) , nrow = 3)
p0 = t(matrix(c(0,0,1) , nrow = 1))

p = matrix(ncol = 6, nrow = 3)
p[,1] = p0

for (i in 2:6){
  p[,i] = t(A) %*% p[,i-1]
}

colnames(p) = paste0("p", 0:5)
p
#output:
     p0   p1     p2       p3        p4        p5
[1,]  0 0.00 0.1875 0.187500 0.2695312 0.2695312
[2,]  0 0.75 0.1875 0.515625 0.2695312 0.4130859
[3,]  1 0.25 0.6250 0.296875 0.4609375 0.3173828