## Probability and Statistics in Matlab

Matlab is a programming environment used a lot within academia and industry. This page provides a guide on how to use random numbers and statistics in Matlab. Matlab it has a lot of predefined advanced functions which speeds up software development. There exists several different toolboxes, which has extra functionality for different fields such as communications, signal processing and finance. There are some key commands which usually are used when working with probability and Matlab, such as the generation and manipulation of Gaussian random variables.

## Gaussian Random Variables in Matlab

To generate Gaussian random variables in Matlab only takes one line of code. To generate a Gaussian random variable, simply type

randn(a,b)

Where a is the number of rows, and b is the number of columns. Hence if we want to generate just a single random variable we type randn(1,1). This will per default generate a variable with zero mean and a variance of one. To modify these parameters, we can simply use

gaussian = mu + sigma*randn(1,1)

This will generate a random variable with mean mu and variance sigma², according for the rules for Gaussians. To view how our variable behaves, we can, for a length of 100, simply type

plot(randn(1,100))

The results from this command is shown in the following figure

Beautiful, isn’t it? With this simple example, we realize the power and simplicity of Matlab. It is possible to create a huge array of complex random-based systems rather easy. Another interesting question is, how would it look if we generated both random x-values and y-values? In other words, how does a Gaussian look in two dimensions? Well, lets type the following command, which generates 5000 random x-values and 5000 random y-values, and see

Very interesting indeed, with this figure, we can see how the Gaussian distribution behaves. It is centered around it’s mean, and only a few values spread outside the dense region in the middle. Please not that this is possible to generate for three dimensions as well, or even more! However, to plot higher than three dimensions is a bit tricky.

## Generate Uniform Random Variable in Matlab

To generate a uniformly distributed random variable, we type

rand(n)

Where this commands generates a n-times-n matrix with uniformly distributed random variables, between 0 and 1. Just like before, to change the limits we can simply manipulate the variable

uniform = (upper-lower)/2 + rand(n)*(upper-lower)

Where “upper” is the upper bound, and “lower” is the lower bound. This simply modification will enable us to generate uniform variables with which ever bounds we prefer.

## Generate Random Vectors in Matlab

Random vectors are extremely important within probability and statistics. What we generated in our first example with the “randn()” function can actually be seen as a random vector. This vector has independent elements, and hence the covariance matrix of this random vector would be a identity matrix.

## Toolboxes in Matlab

There are a lot of great toolboxes for areas such as signal processing, optimization, image processing, control engineering and many other areas. These often provide advanced functionality, however often it is better to write your own functions and scripts, to get a good understanding of underlying concepts. As with all programming, the more code you write, the better you will be.

## Summary

In this tutorial, we learned how to generate Gaussian random variables in Matlab, as well as Uniform random variables. We also learned how to manipulate the Gaussian to change the mean and variance. The Gaussian random variable and the Uniform random variable are the two most fundamental random variable types. We also learned how to how to plot them in Matlab. This should inspire you to experiment in Matlab by yourself and perhaps create some figures to get a good understanding.