 # Basic Statistics in MATLAB®

## Introduction

In this exercise, we briefly present the use of MATLAB® to perform basic statistics. MATLAB is a powerful software program which can simplify a variety of analyses which are commonly performed in engineering. Some general instructions are provided first. An example of how to apply these instructions can be found towards the end of this page.

When you first open MATLAB, you will see the Desktop. It should look approximately as shown below.

The only portion of the Desktop that we will use in this exercise is the Command Window—it is the large window in the middle of the Desktop, which is blank except for the command prompt, >>. As the name implies, the Command Prompt is where we type commands; generally mathematical expressions and functions which we want MATLAB to evaluate.

## Define Variable

To use MATLAB to perform our statistical analysis, we first need to put our resistance data into MATLAB's Workspace. The Workspace is a space in memory which contains all the numbers we will be interested in. Numbers must be in the Workspace before they can be used in calculations1, and the results of any calculations are placed in the Workspace2.

To put our measured resistance data in the Workspace, we will create a variable which contains the resistance numbers. The general syntax to create a variable is3:

• Variable_name = [list of numbers, separated by comma

Variable_name” is the name you choose to call your collection of numbers. The numbers themselves are separated by commas, and contained within square brackets.

## Calculating an Average (Mean) Value

The MATLAB function to calculate the average or mean of a set of numbers is mean. The syntax is:

• mean(Variable_name)

Where Variable_name is the variable which contains the set of numbers we want to average. Typing this at the command prompt and pressing the Enter key will cause MATLAB to determine the average and display it in the command window. By default, the result of the calculation will be assigned to a variable called “ans4.

If you want to save the result of the calculation to a different variable (definitely a good idea), specify the variable name and use an “=” sign:

• Var_mean = mean(Variable_name)

In the above, the result of the calculation will now be in the Workspace under the name “Var_Mean”.

## Calculating a Median Value

The MATLAB function to calculate the median value of a set of numbers is median. The syntax is:

• median(Variable_name)

Where Variable_name is the variable which contains the set of numbers for which we want a median value. Typing this at the command prompt and pressing the Enter key will cause MATLAB to determine the median and display it in the command window. By default, the result of the calculation will be assigned to a variable called “ans”.

If you want to save the result of the calculation to a different variable (definitely a good idea5), specify the variable name and use an “=” sign:

• Var_med = median(Variable_name)

In the above, the result of the calculation will now be in the Workspace under the name “Var_med”.

## Calculating a Standard Deviation

The MATLAB function to calculate the standard deviation of a set of numbers is std. The syntax is:

• std(Variable_name)

Where Variable_name is the variable which contains the set of numbers for which we want a standard deviation. Typing this at the command prompt and pressing the Enter key will cause MATLAB to determine the standard deviation and display it in the command window. By default, the result of the calculation will be assigned to a variable called “ans”.

If you want to save the result of the calculation to a different variable (definitely a good idea), specify the variable name and use an “=” sign:

• Var_std = median(Variable_name)

In the above, the result of the calculation will now be in the Workspace under the name “Var_std”.

## Example

Suppose you have measured the following values:

• 0.3, 0.5, 10, 31.5, and 25

To save them as a variable named “data”, and then calculate the mean, median, and standard deviation of the data, just type the following commands at the command prompt:

• data = [0.3, 0.5, 10, 31.5, 25]
• mean_data = mean(data)
• median_data = median(data)
• stdev_data = std(data)

The result is shown in the figure below: The above screenshots are of MATLAB running on Microsoft Windows 7.

So the mean is 13.4600, the median is 10, and the standard deviation is 14.2455.

• 1 Not entirely true, but it is the way we will do things throughout this page.
• 2 Somewhat closer to being entirely true.
• 3 Variable names in MATLAB cannot contain spaces or “ special ” characters. Stick to letters, numbers, and underscores and you'll be okay. Also, the first character can't be a number.