![]() |
VOOZH | about |
Given a matrix of size M x N, the task is to find the Mean Vector of the given matrix.
Examples:
Input : mat[][] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}}
Output : Mean Vector is [4 5 6]
Mean of column 1 is (1 + 4 + 7) / 3 = 4
Mean of column 2 is (2 + 5 + 8) / 3 = 5
Mean of column 3 is (3 + 6 + 9) / 3 = 6
Input : mat[][] = {{2, 4},
{6, 8}}
Output : Mean Vector is [4 6]
Mean of column 1 is (2 + 6) / 2 = 4
Mean of column 2 is (4 + 8) / 2 = 6
Approach:
Let's take a matrix mat of dimension 5x3 representing lengths, breadths, heights of 5 objects.
Now, the resulting mean vector will be a row vector of the following format :
[mean(length) mean(breadth) mean(height)]
Note: If we have a matrix of dimension M x N, then the resulting row vector will be having dimension 1 x N
Now, simply calculate the mean of each column of the matrix which will give the required mean vector .
Implementation:
[ 4 5 6 ]
Time Complexity: O(m * n), where m and n are the numbers of rows and columns respectively.
Auxiliary Space: O(1)