我正在尝试使用我们所说的负索引来调整矩阵的大小,这意味着它们应该从该矩阵的原点 (0,0) 向左和向上。我试过 grid.resize(-20,-20) 但它不起作用。
任何想法都是有用的。谢谢!
最佳答案
这无论如何都不能就地完成,所以只需创建一个新的(足够大的)矩阵,将原始矩阵复制到更大的矩阵并移动/交换矩阵。示例实现(可以概括为 LeftDown/RightUp/...——此实现还假设 up
和 left
是非负数):
template<class Derived>
void resizeUpLeft(Eigen::DenseBase<Derived>& mat, Eigen::Index up, Eigen::Index left)
{
typename Derived::PlainObject biggerMat(mat.rows()+up, mat.cols()+left);
// Note: biggerMat will be filled with uninitialized values,
// use setZero, if that is not desired
// if allocation of biggerMat fails mat stays unchanged
biggerMat.bottomRightCorner(mat.rows(), mat.cols()) = mat;
// move or swap to avoid copy:
mat.derived() = std::move(biggerMat);
// with C++03:
// mat.derived().swap(biggerMat);
}
关于c++ - 使用 Eigen Library 向上和向左调整矩阵大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51136093/