- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the mean vector of a Matrix in C++
Suppose we have a matrix of order M x N, we have to find the mean vector of the given matrix. So if the matrix is like −
1 | 2 | 3 |
4 | 5 | 6 |
7 | 8 | 9 |
Then the mean vector is [4, 5, 6] As the mean of each column is (1 + 4 + 7)/3 = 4, (2 + 5 + 8)/3 = 5, and (3 + 6 + 9)/3 = 6
From the example, we can easily identify that if we calculate the mean of each column will be the mean vector.
Example
#include<iostream> #define M 3 #define N 3 using namespace std; void calculateMeanVector(int mat[M][N]) { cout << "[ "; for (int i = 0; i < M; i++) { double average = 0.00; int sum = 0; for (int j = 0; j < N; j++) sum += mat[j][i]; average = sum / M; cout << average << " "; } cout << "]"; } int main() { int mat[M][N] = {{ 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; cout << "Mean vector is: "; calculateMeanVector(mat); }
Output
Mean vector is: [ 4 5 6 ]
- Related Articles
- How to find the root mean square of a vector in R?
- Mean and Median of a matrix in C++
- How to generate a normal random vector using the mean of a vector in R?
- How to find the mean of every n values in an R vector?
- How to find the mean of columns of an R data frame or a matrix?
- Number of elements greater than modified mean in matrix in C++
- How to find the mean of a square matrix elements by excluding diagonal elements in R?
- How to find the row and column position of a value as vector in an R matrix?
- PyTorch – How to compute the norm of a vector or matrix?
- How to find mean for x number of rows in a column in an R matrix?
- How to convert a vector into matrix in R?
- How to Create a Vector or Matrix in Python?
- Python – Get Matrix Mean
- How to multiply a matrix with a vector in R?
- Find number of cavities in a matrix in C++

Advertisements