# Matlab Discrete Random Variable

• Sep 29th 2012, 11:54 AM
TwoPlusTwo
Matlab Discrete Random Variable
Hi, I'm trying to simulate a discrete system that has a random input, but I can't figure out how to make the input signal.

Basically, I want to create a vector whose entries are randomly selected from a list of possible values, each with an associated probability.

Is there a simple way of doing this in Matlab?
• Sep 30th 2012, 04:22 PM
TwoPlusTwo
Re: Matlab Discrete Random Variable
Solved. I first made an n-length vector of random numbers between 0 and 1

r=rand(1,n)

and an indexing vector

k=(1:1:n), where n is the desired number of samples.

With p1, p2 etc. being the probabilities for each value of x, the signal becomes:

x(k)=x1*(r(k)<p1)+x2*((r(k)>=p1)&(r(k)<p1+p2))+...

This assigns each possible value of x a portion of the interval between 0 and 1 corresponding to its probability p. For each k an entry from r is checked by all terms. They all evaluate to zero except the one that contains the number from r.