Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
how to sample from a probability distribution in MATLAB

Subject: how to sample from a probability distribution in MATLAB

From: Windell

Date: 2 Sep, 2010 04:27:05

Message: 1 of 2

Hi, I have some sample code that I found online for MATLAB that randomly generates sequences of "weathers" from a state transition function. When I run the code it appears that the "sample" function in the code is not working or is no longer the same.

Is there a way to do what this code does with a current function in MATLAB? Here is the code verbatim.

% MATLAB script
% y = sample(x) returns y sampled from distribution x
X_today = [ 0 1 0 ]'; % Cloudy
T = [ 8 2 0 ; 4 4 2 ; 2 6 2 ]' / 10;
X_tomorrow = sample( T * X_today );
if ( X_tomorrow(1) == 1 )
disp( 'sunny' );
elseif ( X_tomorrow(2) == 1 )
disp( 'cloudy' );
elseif ( X_tomorrow(3) == 1 )
disp( 'rainy' );
end

Subject: how to sample from a probability distribution in MATLAB

From: Neil

Date: 2 Sep, 2010 04:40:22

Message: 2 of 2

"Windell " <windelljones@gmail.com> wrote in message <i5n92p$rf3$1@fred.mathworks.com>...
> Hi, I have some sample code that I found online for MATLAB that randomly generates sequences of "weathers" from a state transition function. When I run the code it appears that the "sample" function in the code is not working or is no longer the same.
>
> Is there a way to do what this code does with a current function in MATLAB? Here is the code verbatim.
>
> % MATLAB script
> % y = sample(x) returns y sampled from distribution x
> X_today = [ 0 1 0 ]'; % Cloudy
> T = [ 8 2 0 ; 4 4 2 ; 2 6 2 ]' / 10;
> X_tomorrow = sample( T * X_today );
> if ( X_tomorrow(1) == 1 )
> disp( 'sunny' );
> elseif ( X_tomorrow(2) == 1 )
> disp( 'cloudy' );
> elseif ( X_tomorrow(3) == 1 )
> disp( 'rainy' );
> end

sample was probably a custom written function, it is not built into MATLAB nor the stats toolbox

To sample and uniform random number generator see:
>>rand
and to sample an normal random number generator see:
>>randn

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us