# [Scilab-users] Is there a way to do it with Matrix Operations?

5 messages
Open this post in threaded view
|

## [Scilab-users] Is there a way to do it with Matrix Operations?

 Dear colleagues:in an attempt to code the generation of random deviates for a user-defined probability function p=[0.1176471    0.2352941    0.0588235    0.3882353    0.2 ], I spent only a few minutes to write the Scilab code below and it gives me all the solutions (frequency distribution of random numbers) that I need.N=100;X=grand(7,N,'def');C=[];for j=1:7;Count(1:5)=0;for k=1:N;i=1;while X(j,k)>P(i);i=i+1;end;Count(i)=Count(i)+1;end;C=[C Count];end;and one typical sample run yields thisC  =   15.    9.     6.     12.    8.     12.    10.     20.    26.    38.    20.    23.    26.    24.     6.     7.     4.     7.     5.     10.    4.      38.    39.    32.    37.    48.    30.    39.     21.    19.    20.    24.    16.    22.    23. However, the for and while loops will be terribly inefficient and this is not good for large scale Monte-Carlo simulations.Is there a way to do it with Matrix Operations?Best greetingsHeinz_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: Is there a way to do it with Matrix Operations?

 Hi Heinz,   Your code example is not working. The index ‘i’ runs out of bounds.   Regards, Rafael     From: users [mailto:[hidden email]] On Behalf Of Heinz Nabielek Sent: Monday, September 25, 2017 2:03 PM To: Users mailing list for Scilab <[hidden email]> Subject: [Scilab-users] Is there a way to do it with Matrix Operations?   Dear colleagues: in an attempt to code the generation of random deviates for a user-defined probability function p=[0.1176471    0.2352941    0.0588235    0.3882353    0.2 ], I spent only a few minutes to write the Scilab code below and it gives me all the solutions (frequency distribution of random numbers) that I need. N=100;X=grand(7,N,'def'); C=[];for j=1:7;Count(1:5)=0;for k=1:N;i=1;while X(j,k)>P(i);i=i+1;end;Count(i)=Count(i)+1;end;C=[C Count];end; and one typical sample run yields this C  =    15.    9.     6.     12.    8.     12.    10.      20.    26.    38.    20.    23.    26.    24.      6.     7.     4.     7.     5.     10.    4.       38.    39.    32.    37.    48.    30.    39.      21.    19.    20.    24.    16.    22.    23. However, the for and while loops will be terribly inefficient and this is not good for large scale Monte-Carlo simulations. Is there a way to do it with Matrix Operations? Best greetings Heinz _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: Is there a way to do it with Matrix Operations?

 On 25.09.2017, at 18:58, Rafael Guerra wrote: > Hi Heinz, >   > Your code example is not working. > The index ‘i’ runs out of bounds. >   > Regards, > Rafael Sorry, I had forgotten the code line -->P=cumsum(p)'; So my question again: can you suggest matrix operations that make my code more efficient? The code is meant to generate random deviates for a user-defined discrete distribution. Below the full set of instructions and example solutions that are good, but achieved in an inefficient way. Heinz -->p=[0.1176471 0.2352941 0.0588235 0.3882353 0.2 ];   -->P=cumsum(p)';   -->N=100;X=grand(7,N,'def');C=[];for j=1:7;Count(1:5)=0;for k=1:N;i=1;while X(j,k)>P(i);i=i+1;end;Count(i)=Count(i)+1;end;C=[C Count];end;C  C  =       9.     10.    13.    9.     12.    6.     19.       32.    21.    15.    18.    22.    24.    20.       10.    4.     3.     12.    4.     9.     6.       37.    45.    47.    39.    44.    42.    38.       12.    20.    22.    22.    18.    19.    17.     -->N=100;X=grand(7,N,'def');C=[];for j=1:7;Count(1:5)=0;for k=1:N;i=1;while X(j,k)>P(i);i=i+1;end;Count(i)=Count(i)+1;end;C=[C Count];end;C  C  =       14.    13.    18.    12.    12.    9.     13.       25.    27.    27.    23.    27.    18.    14.       4.     4.     3.     6.     10.    4.     2.       45.    38.    34.    40.    34.    45.    47.       12.    18.    18.    19.    17.    24.    24.   _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users