Basic Statistics in MATLAB | MATLAB Tutorial
by Ratnesh Shah
Basic Statistics Tutorial
Calculates the Average value of the array
M = mean(A)
M = mean(A,dimension)
% 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)
The matrix would look like
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.
It is the mean of the middle number in the array.
M = median(A): Calculates Median along the column.
M = median(A,2): Calculates Median along the row.
% 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)
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.
Returns the most frequent value in the array.
M = mode(A): Calculates Median along the column.
M = mode(A,2): Calculates Median along the row
% 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)
S = std(M)
S = std(M,w,dim)
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
Using this formula standard deviation can be calculated:
µ is the mean of vector M.
V = var(M)
V = var(M,w,dimension)
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)
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.
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.
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
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
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.
To Find the largest number in the array.
It is similar to that of min function here we have to use max function
% 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
- The element with the maximum value in the array M is 32.
- Maximum elements in columns 1, 2 & 3 are elements 5, 4 & 15 respectively.Maximum elements in rows 1 & 2 are 15 & 8 respectively.