user3551520
user3551520

Reputation: 21

How do I use the MATLAB function randn in c++?

I am trying to implement this code but I am getting the following compiler error: randn wasn't declared in the scope.

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];
}

Upvotes: 1

Views: 943

Answers (1)

The Vivandiere
The Vivandiere

Reputation: 3191

You need Matlab Compiler SDK for this. Using the Matlab Compiler SDK, you will be able to export the specific Matlab functionality you need, in your case perhaps randn function. The Compiler SDK generates .dll, .lib, and .h files for the Matlab function you want to export. You can then call the function in the shared library from C++ using the mwArray library built specifically for marshalling data between Matlab generated dll and your C++ code. All of this is documented here.

P.S. I heard that they recently separated Matlab Compiler from the Matlab Compiler SDK, so be careful about what you purchase.

This is strictly an answer to your question as stated, random number generation facilities in C++ 11 will probably serve you best as @drescherjm correctly points out in the comments.

Upvotes: 1

Related Questions