2
我想创建一个递归函数,它将执行矩阵乘法运算n
次。递归函数返回错误消息
我的代码如下:
R <- function(P, n){
R(P, n-1) %*% P
}
当这个函数被调用,n = 3
执行
(P %*% P) %*% P.
使用一个例子,我会想到:当我
> P
[,1] [,2] [,3]
[1,] 0.6 0.1 0.3
[2,] 0.2 0.7 0.1
[3,] 0.3 0.3 0.4
然而调用函数我收到一条错误消息。
Error: evaluation nested too deeply: infinite recursion/options(expressions=)?
Error during wrapup: evaluation nested too deeply: infinite recursion/options(expressions=)?
您能向我解释为什么函数不起作用以及应如何修改代码?
OP的数据:'P < - 结构(C(0.6,0.2,0.3,0.1,0.7,0.3,0.3,0.1,0.4),.dim伪= C(3L, 3L) )' – 989