Open In App

Variance and standard-deviation of a matrix

Prerequisite – Mean, Variance and Standard Deviation, Variance and Standard Deviation of an array
Given a matrix of size n*n. We have to calculate variance and standard-deviation of given matrix. 

Examples : 

Input : 1 2 3
        4 5 6
        6 6 6 
Output : variance: 3
         deviation: 1

Input : 1 2 3
        4 5 6
        7 8 9 
Output : variance: 6
         deviation: 2  

Explanation: 

First mean should be calculated by adding sum of each elements of the matrix. After calculating mean, it should be subtracted from each element of the matrix.Then square each term and find out the variance by dividing sum with total elements. 

Deviation: It is the square root of the variance.

Example: 

      1 2 3
      4 5 6
      7 8 9

Here mean is 5 and variance is approx 6.66

Below is code implementation:

























Output : 
Mean: 5
Variance: 6
Deviation: 2

 

Time Complexity: O(n*n)
Auxiliary Space: O(1)

 


Article Tags :