zhou1_20 发表于 2019-4-11 21:19 
参考下吧
再向您请教一个问题:
代码中for循环:
for (k in 3:m) {
p[,k+1]=p[,2]*p[,k]-(k^2*(n^2-k^2))/(4*(4*k^2-1))*p[,k-1]
}
是否可以使用sapply函数替代。
我试验了多种方式:

但都不能完成以上for循环的功能。
在查看sapply函数帮助文件时,我发现这么一段:
Simplification in sapply is only attempted if X has length greater than zero and if the return values from all elements of X are all of the same (positive) length. If the common length is one the result is a vector, and if greater than one is a matrix with a column corresponding to each element of X.
代码sapply中的x=3:m,x每个元素公共长度为1,一次sapply只能返回一个向量,所以产生了上述图片中的错误,请问是这个原因吗?应如何去解决上面的问题呢?