Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C/++/# [Convert Matlab code into C++]

  1. Jun 15, 2016 #1
    anyone help me to convert this sample code of Gaussian Cone shape of random number code in Matlab into C++

    Code (Matlab M):

    function GaussianCone
        clear all; clc;

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


        sigma=0.01; R=0.5; mu=0;

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

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

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


    Last edited by a moderator: Jun 15, 2016
  2. jcsd
  3. Jun 15, 2016 #2


    Staff: Mentor

  4. Jun 15, 2016 #3

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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.
  5. Jun 15, 2016 #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 (Text):

    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);
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted