C++ 中的 Eigen MatrixXd 回推
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14130699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Eigen MatrixXd push back in c++
提问by Fantastic Mr Fox
Eigen is a well known matrix Library in c++. I am having trouble finding an in built function to simply push an item on to the end of a matrix. Currently I know that it can be done like this:
Eigen 是一个众所周知的 C++ 矩阵库。我很难找到一个内置函数来简单地将一个项目推到矩阵的末尾。目前我知道可以这样做:
Eigen::MatrixXd matrix(10, 3);
long int count = 0;
long int topCount = 10;
for (int i = 0; i < listLength; ++i) {
matrix(count, 0) = list.x;
matrix(count, 1) = list.y;
matrix(count, 2) = list.z;
count++;
if (count == topCount) {
topCount *= 2;
matrix.conservativeResize(topCount, 3);
}
}
matrix.conservativeResize(count, 3);
And this will work (some of the syntax may be out). But its pretty convoluted for a simple thing to do. Is there already an in built function?
这将起作用(某些语法可能已失效)。但对于一件简单的事情来说,它非常复杂。是否已经有内置功能?
回答by David Brown
There is no such function for Eigen matrices. The reason for this is such a function would either be very slow or use excessive memory.
特征矩阵没有这样的函数。这样做的原因是这样的函数要么非常慢,要么使用过多的内存。
For a push_back
function to not be prohibitively expensive it must increase the matrix's capacity by some factor when it runs out of space as you have done. However when dealing with matrices, memory usage is often a concern so having a matrix's capacity be larger than necessary could be problematic.
If it instead increased the size by rows()
or cols()
each time the operation would be O(n*m)
. Doing this to fill an entire matrix would be O(n*n*m*m)
which for even moderately sized matrices would be quite slow.
对于一个push_back
不那么昂贵的函数,它必须像您所做的那样在空间不足时将矩阵的容量增加一些因素。然而,在处理矩阵时,内存使用通常是一个问题,因此矩阵的容量大于必要的可能会出现问题。如果它改为将大小增加rows()
或cols()
每次操作将是O(n*m)
. 这样做来填充整个矩阵O(n*n*m*m)
对于即使是中等大小的矩阵也会很慢。
Additionally, in linear algebra matrix and vector sizes are nearly always constant and known beforehand. Often when resizeing a matrix you don't care about the previous values in the matrix. This is why Eigen's resize
function does not retain old values, unlike std::vector
's resize
.
此外,在线性代数中,矩阵和向量的大小几乎总是恒定的并且事先已知。通常在调整矩阵大小时,您并不关心矩阵中先前的值。这就是为什么 Eigen 的resize
函数不像std::vector
's那样保留旧值resize
。
The only case I can think of where you wouldn't know the matrix's size beforehand is when reading from a file. In this case I would either load the data first into a standard container such as std::vector
using push_back
and then copy it into an already sized matrix, or if memory is tight run through the file once to get the size and then a second time to copy the values.
我能想到的唯一一种您事先不知道矩阵大小的情况是从文件中读取时。在这种情况下,我会首先将数据加载到标准容器中,例如std::vector
usingpush_back
然后将其复制到一个已经确定大小的矩阵中,或者如果内存很紧,则通过文件运行一次以获取大小,然后第二次复制值.
回答by Yuushi
There is no such function, however, you can build something like this yourself:
没有这样的功能,但是,您可以自己构建这样的东西:
using Eigen::MatrixXd;
using Eigen::Vector3d;
template <typename DynamicEigenMatrix>
void push_back(DynamicEigenMatrix& m, Vector3d&& values, std::size_t row)
{
if(row >= m.rows()) {
m.conservativeResize(row + 1, Eigen::NoChange);
}
m.row(row) = values;
}
int main()
{
MatrixXd matrix(10, 3);
for (std::size_t i = 0; i < 10; ++i) {
push_back(matrix, Vector3d(1,2,3), i);
}
std::cout << matrix << "\n";
return 0;
}
If this needs to perform too many resizes though, it's going to be horrendously slow.
如果这需要执行太多的调整大小,它会非常慢。