Open In App

Variance and standard-deviation of a matrix

Last Updated : 13 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:

C++





Java





Python3





C#





PHP





Javascript





Output : 

Mean: 5
Variance: 6
Deviation: 2

 

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

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads