c++ 求输入的矩阵的协方差矩阵

这篇博客介绍了如何使用C++计算输入的矩阵的协方差矩阵。作者推荐手动求解示例,并提供了详细的代码及使用方法。用户需要在inputData.txt文件中输入数据,并在运行时指定矩阵的行数和列数。
摘要由CSDN通过智能技术生成

参考知识点:
1、这个说的非常详细:协方差矩阵计算方法
( 推荐手动求解他的例子)

代码:

#include <iostream>
#include<vector>
#include<fstream>
using namespace std;
int main()
{
   
	int m, n;
	cout << "请输入矩阵的行、列数:" << endl;
	cin >> m >> n;
	vector<vector<double>> matrix(m, vector<double>(n));
	vector<vector<double>> matrixT(n, vector<double>(m));
	cout << "输入矩阵中:" << endl;
	ifstream fin("inputData.txt"
以下是使用 C++ 取 6*6 矩阵协方差和特征值的示例代码: ```c++ #include <iostream> #include <cmath> #include <iomanip> using namespace std; // 矩阵均值 void getMean(double matrix[6][6], double mean[6]) { for (int j = 0; j < 6; j++) { double sum = 0; for (int i = 0; i < 6; i++) { sum += matrix[i][j]; } mean[j] = sum / 6.0; } } // 协方差矩阵 void getCovariance(double matrix[6][6], double covariance[6][6], double mean[6]) { for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { double sum = 0; for (int k = 0; k < 6; k++) { sum += (matrix[k][i] - mean[i]) * (matrix[k][j] - mean[j]); } covariance[i][j] = sum / 5.0; } } } // 打印矩阵 void printMatrix(double matrix[6][6]) { for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { cout << setw(10) << matrix[i][j]; } cout << endl; } } // 取特征值和特征向量 void getEigenValue(double matrix[6][6], double eigenvalue[6], double eigenvector[6][6]) { double eps = 1e-10; double err = 1.0; int max_iter = 200; int n = 6; // 初始化特征向量 for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { eigenvector[i][j] = (i == j) ? 1 : 0; } } // 迭代解特征值和特征向量 int iter = 0; while (err > eps && iter < max_iter) { double sum = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { sum += pow(matrix[i][j], 2); } } err = sqrt(sum); for (int p = 0; p < n; p++) { for (int q = p + 1; q < n; q++) { double app = matrix[p][p]; double apq = matrix[p][q]; double aqq = matrix[q][q]; double phi = 0.5 * atan2(2 * apq, aqq - app); double c = cos(phi); double s = sin(phi); matrix[p][p] = c * c * app + 2 * c * s * apq + s * s * aqq; matrix[q][q] = s * s * app - 2 * c * s * apq + c * c * aqq; matrix[p][q] = 0; matrix[q][p] = 0; for (int k = 0; k < n; k++) { if (k != p && k != q) { double akp = matrix[k][p]; double akq = matrix[k][q]; matrix[k][p] = c * akp + s * akq; matrix[p][k] = matrix[k][p]; matrix[k][q] = -s * akp + c * akq; matrix[q][k] = matrix[k][q]; } } for (int k = 0; k < n; k++) { double epi = eigenvector[k][p]; double eqi = eigenvector[k][q]; eigenvector[k][p] = c * epi + s * eqi; eigenvector[k][q] = -s * epi + c * eqi; } } } iter++; } for (int i = 0; i < n; i++) { eigenvalue[i] = matrix[i][i]; } } int main() { double matrix[6][6] = { { 1, 2, 3, 4, 5, 6 }, { 2, 3, 4, 5, 6, 7 }, { 3, 4, 5, 6, 7, 8 }, { 4, 5, 6, 7, 8, 9 }, { 5, 6, 7, 8, 9, 10 }, { 6, 7, 8, 9, 10, 11 } }; double mean[6]; double covariance[6][6]; double eigenvalue[6]; double eigenvector[6][6]; // 矩阵均值 getMean(matrix, mean); // 协方差矩阵 getCovariance(matrix, covariance, mean); // 打印协方差矩阵 cout << "协方差矩阵:" << endl; printMatrix(covariance); // 取特征值和特征向量 getEigenValue(covariance, eigenvalue, eigenvector); // 打印特征值 cout << "特征值:" << endl; for (int i = 0; i < 6; i++) { cout << eigenvalue[i] << endl; } // 打印特征向量 cout << "特征向量:" << endl; printMatrix(eigenvector); return 0; } ``` 该示例代码中,首先矩阵均值,然后协方差矩阵,接着使用迭代法取特征值和特征向量。最后打印协方差矩阵、特征值和特征向量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值