我正在尝试实现此代码,但出现以下编译器错误:randn 未在范围内声明
。
void levyflt()
{
int j;
double beta=1.5,sigma,rand_num;
sigma=pow((tgamma(1+beta)*sin(M_PI*beta/2)/(tgamma((1+beta)/2)*beta*pow(2,((beta-1)/2)))),(1/beta));
for(j=0;j<d;j++)
{
rand_num=randn(-1,1);
u[j]=rand_num*sigma;
rand_num=randn(-1,1);
v[j]=rand_num;
step[j]=u[j]/pow(fabs(v[j]),(1/beta));
L[j]=0.01*step[j];
}
cout<<L[j];
}
最佳答案
为此您需要 Matlab Compiler SDK。使用 Matlab 编译器 SDK,您将能够导出您需要的特定 Matlab 功能,在您的情况下可能是 randn
功能。 Compiler SDK 为您要导出的 Matlab 函数生成 .dll、.lib 和 .h 文件。然后,您可以使用专门为在 Matlab 生成的 dll 和您的 C++ 代码之间编码数据而构建的 mwArray
库从 C++ 调用共享库中的函数。所有这些都记录在案 here .
附言我听说他们最近将 Matlab Compiler 从 Matlab Compiler SDK 中分离出来,所以购买时要小心。
正如@drescherjm 在评论中正确指出的那样,这严格来说是对您问题的回答,C++ 11 中的随机数生成工具可能最适合您。
关于c++ - 如何在 C++ 中使用 MATLAB 函数 randn?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32529607/