vendredi 19 janvier 2018

C vs MATLAB (make a matrix whose elements are random numbers uniformly distributed)

I'm running the simulator with MATLAB. However, it takes a few days. Hence, I decided to change the code into C. (First, I tried to use c-mex in MATLAB, but I think coding and debugging are very hard. mxType!!!?!?!? Thus, I decided to make C code using the visual studio 2017.)

In my MATLAB code, I used

x = [unifrnd(varmin(1),varmax(1),varnum,1),...
     unifrnd(varmin(2),varmax(2),varnum,1),...
     unifrnd(varmin(3),varmax(3),varnum,1)];

That is, x is the matrix of size varnum*3, whose 1st column is random numbers uniformly distributed from varmin(1) to varmax(1), 2nd column is random numbers uniformly distributed from varmin(2) to varmax(2), and 3rd column is random numbers uniformly distributed from varmin(3) to varmax(3).

When I make a matrix in C code, I will code like the follows:

srand(time(NULL));
for(j=1; j<3; j++) {
    for(i=1; i<varnum; i++) {
        x[i][j] = rand() % (varmax[j]-varmin[j]) + varmin[j];
    }
}

I am changing my code from MATLAB to C because the running time is so long. However, I think MATLAB handles a matrix at once, but C handles a matrix of size MxN by running M*N iteration. Nonetheless, is the code below (C) faster than the code above (MATLAB)? Also, is there any better codes to make a matrix having random numbers?




Aucun commentaire:

Enregistrer un commentaire