特征矩阵取矩阵的每一行的平均值(计算列向量的质心) [英] Eigen class to take mean of each row of matrix (compute Centroid of the column vectors)
本文介绍了特征矩阵取矩阵的每一行的平均值(计算列向量的质心)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我真的是C ++的新手,但这就是我想要做的。我有一个4 x 3矩阵:
I am really new with C++ but here is what I am trying to do. I have a 4 by 3 matrix:
100 109.523 119.096
100 89.7169 76.256
100 96.0822 103.246
100 101.084 85.0639
我想计算每行的平均值并将其存储在向量。我正在使用本征库。我想不出任何有效的方法。到目前为止,这是我的代码:
I want to take calculate the mean of each row and store it in some vector. I am using the Eigen library. I cannot think of anything to do this efficiently. Here is my code thus far:
MatrixXd SS(N,n+1);
MatrixXd Z = generateGaussianNoise(N,n);
for(int i = 0; i < N; i++){
SS(i,0) = S0;
for(int j = 1; j <= n; j++){
SS(i,j) = SS(i,j-1)*exp((double) (r - pow(sigma,2.0))*dt + sigma*sqrt(dt)*(double)Z(i,j-1));
}
}
cout << SS << endl;
cout << endl;
VectorXd S_A(3);
S_A = SS.row(1);
所以我拥有的是4 x 3矩阵 SS
,现在我想取每一行的均值并将其存储在向量 S_A
中。我对此有很多困难,因此任何建议将不胜感激。
So what I have is a 4 by 3 matrix SS
and now I want to take the mean of each row and store it in the vector S_A
. I having a lot of difficulty with this, so any suggestions would be greatly appreciated.
推荐答案
您要部分减少:
Vector3d S_A = SS.rowwise().mean();
这篇关于特征矩阵取矩阵的每一行的平均值(计算列向量的质心)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文