Cho ma trận M x N. Vector trung bình là mảng chứa trung bình của từng cột. Ví dụ ma trận 3x3:\n1 2 3\n4 5 6\n7 8 9\n→ Vector trung bình: [4, 5, 6] vì (1+4+7)/3=4, (2+5+8)/3=5, (3+6+9)/3=6.
Ví dụ
#include <iostream>
#define M 3
#define N 3
using namespace std;
void calculateMeanVector(int mat[M][N]) {
cout << "[ ";
for (int i = 0; i < N; i++) {
double average = 0.0;
int sum = 0;
for (int j = 0; j < M; j++)
sum += mat[j][i];
average = sum / (double)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);
}
Kết quả
Mean vector is: [ 4 5 6 ]