在C ++中找到矩阵的均值向量
假设我们有一个M×N阶的矩阵,我们必须找到给定矩阵的均值向量。所以如果矩阵像-
那么平均向量为[4、5、6],因为每列的平均值为(1+4+7)/3=4,(2+5+8)/3=5和(3+6+9)/3=6
从示例中,我们可以轻松地确定,如果我们计算每列的均值将是均值向量。
示例
#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);
}输出结果
Mean vector is: [ 4 5 6 ]