简体   繁体   English

如何将两个 Eigen::Vector3f 除以相应的元素

[英]How can I divide two Eigen::Vector3f by the corresponding elements

I need to divide two vectors by the corresponding elements.我需要将两个向量除以相应的元素。 How can I accomplish this?我怎样才能做到这一点? I couldn't find any good sources.我找不到任何好的资源。 Something like就像是

Eigen::Vector3f v1 = { 10.0f, 10.0f, 10.0f };
Eigen::Vector3f v2 = { 5.0f, 2.0f, 2.0f };
Eigen::Vector3f v3 = v1 / v2;

Expected result:预期结果:

{ 2.0f, 5.0f, 5.0f }

It says "no operator / matches the operands" for division.它说除法“没有运算符/匹配操作数”。

While the builtin matrix (expression) types support common linear algebra operations through overloaded operators (eg matrix-vector multiplication), Eigen provides distinct types for component-wise operations;虽然内置矩阵(表达式)类型通过重载运算符(例如矩阵向量乘法)支持常见的线性代数运算,但 Eigen 为组件运算提供了不同的类型; the are called "arrays" and subsume Eigen::Array<...> instantiations as well as array expressions.它们被称为“数组”并包含Eigen::Array<...>实例化以及数组表达式。 You can always wrap a matrix into an array expression and vice-versa by the .array() and .matrix() member functions - they don't copy anything, but repackage the underlying data such that array-associated operations can be used for matrices, or matrix-associated operations for arrays. In your case, this would be您始终可以通过.array().matrix()成员函数将矩阵包装到数组表达式中,反之亦然 - 它们不复制任何内容,而是重新打包底层数据,以便可以使用与数组相关的操作arrays 的矩阵或矩阵相关操作。在您的情况下,这将是

const Eigen::Vector3f v1 = { 10.0f, 10.0f, 10.0f };
const Eigen::Vector3f v2 = { 5.0f, 2.0f, 2.0f };
const Eigen::Vector3f v3 = v1.array() / v2.array();

If you don't need the matrix interface at all, you can also use Eigen::Array<...> directly:如果你根本不需要矩阵接口,你也可以直接使用Eigen::Array<...>

const Eigen::Array3f a1 = { 10.0f, 10.0f, 10.0f };
const Eigen::Array3f a2 = { 5.0f, 2.0f, 2.0f };
const Eigen::Array3f a3 = a1 / a2;

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

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