[Convert Matlab code into C++]

In summary, the conversation discusses converting a sample code for a Gaussian Cone shape of random numbers from MATLAB to C++, with the possibility of using Julia as an alternative. The conversation also suggests using a graphics library and taking advantage of C++'s random number generation capabilities. The code is also simplified in MATLAB before conversion.
  • #1
MAKK
4
0
anyone help me to convert this sample code of Gaussian Cone shape of random number code in Matlab into C++

Matlab:
function GaussianCone
    clear all; clc;

    mx=10000; my=mx; mz=mx;

    z=[1:1:mz]';

    sigma=0.01; R=0.5; mu=0;
    sigmax=sigma+R*z;
    sigmay=sigma+R*z;

    x=zeros(mx,1); y=zeros(my,1);

    for i=1:mx
        x(i)=normrnd(mu, sigmax(i)); 
    end

    for j=1:my
        y(j)=normrnd(mu, sigmay(j));
    end

    size(x)
    size(y)
    size(z)

    plot3(x,z,y,'.r')
end
 
Last edited by a moderator:
Physics news on Phys.org
  • #3
You have graphics in your code, so you're going to need some kind of c++ graphics library to make the conversion complete. The c++ standard itself does not address graphics. I'd suggest learning to use Qt, which is dual licensed (commercial and open source, with some restrictions on open source usage).

You also have normally distributed random numbers in your code. C++11 (and later releases) has very nice random number generation capabilities. You'll want to take advantage of those. For example, see http://en.cppreference.com/w/cpp/numeric/random/normal_distribution.
 
  • #4
Your code could be simplified in MATLAB first. There is no reason to ever issue a clear all command as the first line of a function in MATLAB, because there is nothing to clear in the function's workspace except any input arguments (when passed).

Code:
function GaussianCone

mx = 10000;
my = mx;
mz = mx;
z = (1:mz)';
sigma = 0.01;
R = 0.5;
mu = 0;
sigmax = sigma+R*z;
sigmay = sigma+R*z;
x = normrnd(mu,sigmax);
y = normrnd(mu,sigmay);
plot3(x,z,y,'.r')
 

1. How do I convert my Matlab code into C++ code?

To convert your Matlab code into C++ code, you can use the Matlab Coder tool. This tool automatically generates C++ code from your Matlab code, making the conversion process much easier. Alternatively, you can manually rewrite your code in C++ using the same logic and algorithms as your Matlab code.

2. Will my converted C++ code have the same functionality as my original Matlab code?

In most cases, the converted C++ code will have the same functionality as your original Matlab code. However, there may be some differences due to the differences in syntax and libraries used in Matlab and C++. It is important to thoroughly test your converted code to ensure it functions as intended.

3. Can I convert any Matlab code into C++?

Yes, you can convert any Matlab code into C++ as long as it does not use any Matlab-specific functions or toolboxes. The Matlab Coder tool is compatible with most commonly used functions in Matlab, but some complex functions may not be supported.

4. Are there any benefits to converting Matlab code into C++?

Yes, there are several benefits to converting Matlab code into C++. C++ is a faster and more efficient language, making it ideal for applications that require high performance. Additionally, C++ is a widely used language in industries such as finance, engineering, and data analysis, making it easier to collaborate and share code with others.

5. Can I still use Matlab after converting my code into C++?

Yes, converting your code into C++ does not affect your ability to use Matlab. You can still use Matlab for data analysis and visualization, while using the converted C++ code for applications that require faster performance.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
129
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
576
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
270
Back
Top