How to Convert Matlab Gaussian Cone Code to C++?

Click For Summary

Discussion Overview

The discussion revolves around converting a MATLAB function that generates a Gaussian cone shape using random numbers into C++. The conversation touches on various aspects of the conversion process, including graphics handling and random number generation.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant requests assistance in converting MATLAB code for generating a Gaussian cone into C++.
  • Another participant questions the motivation behind the conversion, suggesting it might be related to performance, and proposes considering Julia as an alternative.
  • A participant points out the need for a graphics library in C++ since the standard does not include graphics support, recommending Qt for this purpose.
  • It is noted that C++11 and later versions offer improved random number generation capabilities, with a reference provided for normal distribution in C++.
  • One participant suggests simplifying the MATLAB code before conversion, specifically mentioning the unnecessary use of 'clear all' in the function.

Areas of Agreement / Disagreement

Participants express differing views on the necessity of the conversion and the tools to be used, indicating that multiple competing perspectives remain regarding the best approach to the task.

Contextual Notes

The discussion includes assumptions about the need for graphics handling in C++, the potential benefits of simplifying the MATLAB code, and the capabilities of C++11 for random number generation, which may not be fully resolved.

MAKK
Messages
4
Reaction score
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
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.
 
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')
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
Replies
5
Views
3K
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 11 ·
Replies
11
Views
5K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 41 ·
2
Replies
41
Views
10K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K