本征:将每一行除以最后一行 [英] Eigen: Divide each row by last row
问题描述
在使用Eigen的 rowwise
操作时,我不太清楚这个语法。
I can't quite figure out the syntax for this when using Eigen's rowwise
operations...
我有一个本征矩阵,我想将每一行除以最后一行。因此,如果我们从矩阵开始
I have an Eigen matrix, and I want to divide each row by the last row. So if we started with a matrix
r = [ 0, 1
2, 3
4, 5 ]
然后在进行此转换后,我想拥有
then after this transform, I want to have
r = [ 0, .2
.5, .6
1, 1 ]
最好执行该操作,覆盖 r
。此外,我不会使用最后一行中的值,因此,最后一行实际上在转换后是否为1都无关紧要。
Preferably the operation would happen in place, overwriting r
. Furthermore, I will not be using the values in the last row, so it doesn't matter if the last row is actually 1's after the transform.
以下是我尝试过的一些无法编译的语法:
Here are some syntaxes I have tried that do not compile:
r.rowwise() = (r.array().rowwise() / r.bottomRows(1).array()).eval();
r.rowwise() = (r.rowwise().array() / r.bottomRows(1).array()).eval();
r.rowwise() /= r.bottomRows(1).array();
r = r.rowwise().cwiseQuotient(rrr);
此普通的for循环版本有效
This plain old for-loop version works
int last_row = r.rows() - 1;
for (int row = 0; row < last_row; ++row) {
r.row(row).array() /= r.row(last_row).array();
}
但是,无论我走到哪里,人们都在倡导使用 rowwise
或 colwise
操作。我无法使用该语法。我想使用 rowwise
运算符做一个简洁的形式吗?
However, everywhere I turn, people are advocating using the rowwise
or colwise
operations. I can't get this to work with that syntax. Is there a nice concise form of what I want to do using the rowwise
operator?
推荐答案
要完成自我回答,如果不需要最后一行,可以使用已标准化:
To complete the self-answer, in case you don't need the last row, you can use hnormalized:
result = r.colwise().hnormalized()
并带有Eigen主干您还可以使用命名空间Eigen :: placeholders :: last;
and with Eigen trunk you can also write:
using namespace Eigen::placeholders::last;
r.array().rowwise() /= r.row(last).array();
这篇关于本征:将每一行除以最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!