简体   繁体   English

将标量添加到特征矩阵(向量)

[英]Adding scalar to Eigen matrix (vector)

I just started using Eigen library and can't understand how to add a scalar value to all matrix's members?我刚开始使用 Eigen 库,不明白如何为所有矩阵的成员添加标量值?

Let's suppose that I have a matrix:假设我有一个矩阵:

Eigen::Matrix3Xf mtx = Eigen::Matrix3Xf::Ones(3,4);
mtx = mtx + 1;    // main.cxx:104:13: error: invalid operands to binary expression ('Eigen::Matrix3Xf' (aka 'Matrix<float, 3, Dynamic>') and 'int')

I expect that the resulting matrix would be filled with 2我希望得到的矩阵将填充 2

Element-wise operations with Eigen are best done in the Array domain.使用 Eigen 的元素操作最好在Array域中完成。 You can do你可以做

mtx.array() += 1.f;

A slightly more verbose option would be:稍微详细一点的选项是:

mtx += Eigen::Matrix3Xf::Ones(3,4);

You should also consider defining mtx as an Array3Xf in the first place:您还应该首先考虑将mtx定义为Array3Xf

Array3Xf mtx = Eigen::Array3Xf::Ones(3,4);
mtx += 1.f;

If you then need to use mtx as an matrix (ie, in a matrix product), you can write如果您随后需要将mtx用作矩阵(即,在矩阵乘积中),您可以编写

Vector3f v = mtx.matrix() * w; 

Doing a quick search on the documentation of this library it seems there is no such method.快速搜索这个库的文档,似乎没有这样的方法。 In fact, matrix algebra does not generally have a scalar sum.事实上,矩阵代数一般没有标量和。 You could implement such a method your self, just by adding the scalar to each matrix i,j component iterating through all the columns and rows.您可以自己实现这样的方法,只需将标量添加到遍历所有列和行的每个矩阵 i,j 组件即可。

However are you sure you weren't meaning to do a scalar multiplication?但是,您确定您不是要进行标量乘法吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM