繁体   English   中英

用图模拟n个矩阵样本

[英]simulating n matrix samples with plots

我想创建一个函数,允许用户选择一个nxn矩阵,并用0和1的样本填充它,查找TIC TAC TOE“ wins”(连续,col或diag中的所有1或0),并根据随机样本绘制获胜解决方案的概率图。 我希望用户能够运行任意数量的模拟。

我已经有用于创建单个矩阵的代码

ranmat = function(nrow,ncol){
matrix(sample(c(0,1),replace=T,size=nrow*ncol),nrow=nrow)

我也有关于如何检查获胜的想法,但是我不知道如何进行模拟n次。

我认为您正在寻找for-loop? 我认为有很多方法可以做您想做的事情。

# 1st option: you might want to call your function n times:
# with l,k and n being integer values (n is the number of simulation)
for(i in 1:n) ranmat(nrow=k,ncol=l) 

# 2nd option: you tell your function how often you want to simulate:
ranmat = function(nrow,ncol,nsimult){
    for(i in 1:nsimult){
        # your simulations
    }
    return(result) # with result being an appropriate way to return the outcome of your simulations
}
ranmat(nrow=k,ncol=l,nsimult=n) #call to your function with k,l and n being integer values

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM