# Basic Statistics in MATLAB | MATLAB Tutorial

## Mean

Calculates the Average value of the array

Syntax:

M = mean(A)
M = mean(A,dimension)

### Example:

```% Computes the mean of each column

M = [1,3,5;2,4,6];
mean_column=mean(M)
% Computes the mean of each row
A=[1,3,5;2,4,6];
mean_row = mean(A,2)

```

### Explanation:

The matrix would look like

M =

1            3            5

2            4            6

The Mean of 1st Column can be calculated as (1 + 2)/2 = 1.5
The Mean of 1st row can be calculated as (1 + 3 + 5)/3 = 3
Others can be calculated similarly.

## Median

It is the mean of the middle number in the array.

Syntax:

M = median(A):  Calculates Median along the column.
M = median(A,2): Calculates Median along the row.

### Example:

```% Input Matrix
M = [0 1 1; 2 3 2; 1 3 2; 4 2 2]
% Computes Median along the column
median_column = median(M)
% Computes Median along the row
median_row = median(M,2)```

### Explanation:

For each column, the median value is the mean of the middle two numbers in sorted order.
The Median of the 1st column can be calculated as (1+2)/2 = 1.5
Similarly, the median for the 3rd row first sorting it would give us the middle element as 2.

## Mode

Returns the most frequent value in the array.

Syntax:

M = mode(A): Calculates Median along the column.
M = mode(A,2): Calculates Median along the row

### Example:

```% Input Matrix
M = [0 1 1; 0 2 2; 3 2 3; 4 4 2]
%Computes Mode along the column
Mode_column = mode(M)
%Computes Mode along the row
Mode_row = mode(M,2)```

## Standard Deviation

Syntax:

S = std(M)
S = std(M,w,dim)

### Example:

Calculate the Standard Deviation of the given Matrix.

```M = [2 1 -3; 5 5 -3; 9 7 -1] %Given Matrix
Col_Std = std(M) % Computes Standard Deviation of each column.
M2 = [7 5 24 -4; 10 -11 5 12; 3 9 -6 2] %Given Matrix
Row_Std = std(M2,0,2) % Computes Standard Deviation of each row```

### Explanation:

Using this formula standard deviation can be calculated:

µ is the mean of vector M.

## Variance

Syntax:

V = var(M)
V = var(M,w,dimension)

### Example:

Calculate the variance of the given matrix.

```M = [2 1 -3; 5 5 -3; 9 7 -1]
%Computes the variance along the column
col_var = var(M)
%Here 2 states the dimension along which the variance needs to be computed
row_var = var(M,0,2)```

### Explanation:

As per given formula the variance can be calculated. It is the square of standard deviation. It can be checked from the previous result of standard deviation for column. We got variance as 12.3333 which is (3.5119)2.

Formula:

## Minimum

To find the smallest number in the array.

• If M is a matrix, then min(M) is a row vector containing the minimum value of each column.
• If M is a vector, then min(M) returns the smallest element of M.
• If M is a multidimensional array, then min(M) operates along the first array dimension whose size does not equal 1, treating the elements as vectors. The size of this dimension becomes 1 while the sizes of all other dimensions remain the same. If M is an empty array with first dimension 0, then min(M) returns an empty array with the same size as M.

### Example:

Find the Minimum element

```M = [10 15 5 32] % Given vector
vec_min = min(M) % result 1
M1 = [1 2 15;5 4 8] % Given Matrix M1
mat_min_col = min(M1)% Returns the minimum element in each column
mat_min_row = min(M1,[],2)% Returns minimum element in each row here 2 denotes the dimension ```

### Explanation:

As can be seen minimum element in M = [10 15 5 32] is 5.

The matrix M1 = [1 2 15 ; 5 4 8] can be seen as
M1=

1    2     15

5    4     8

As can be seen from the above matrix the minimum element in each column are 1, 2 & 8 respectively.
Similarly, for each row the answer would be 1, 4 respectively.

## Maximum

To Find the largest number in the array.

It is similar to that of min function here we have to use max function

### Example:

```% Find the maximum element
M = [10 15 5 32] % Given vector
vec_max = max(M) % result 1
M1 = [1 2 15;5 4 8] % Given Matrix M1
mat_max_col = max(M1)% Returns the maximum element in each column
mat_max_row = max(M1,[],2)% Returns maximum element in each row here 2 denotes the dimension
```

### Explanation:

1. The element with the maximum value in the array M is 32.
2. Maximum elements in columns 1, 2 & 3 are elements 5, 4 & 15 respectively.Maximum elements in rows 1 & 2 are 15 & 8 respectively.

##### Ratnesh Shah

Experienced Programmer skilled in Machine Learning, MATLAB, Java, MySQL, and MongoDB. Strong engineering professional persuing a Bachelor’s Degree focused in Information and Communication Technology from School of Engineering & Applied Sciences, Ahmedabad University.

### Recommended Posts

##### Counters using conditionally executed subsystems | Simulink Tutorial

16 Feb 2018 - Simulink, Tutorial

##### Regulating drum boiler pressure

16 Feb 2018 - Simulink, Tutorial