Fyt*_*tch 3 c++ performance matrix linear-algebra
我做了一个矩阵类,我想实现一个转置方法:
template<typename T>
void Matrix<T>::Transpose()
{
// for square matrices
if(this->Width() == this->Height())
{
for(std::size_t y = 1; y < this->Height(); ++y)
{
for(std::size_t x = 0; x < y; ++x)
{
// the function operator is used to access the entries of the matrix
std::swap((*this)(x, y), (*this)(y, x));
}
}
}
else
{
// TODO
}
}
Run Code Online (Sandbox Code Playgroud)
问题是如何在不分配全新矩阵(该类用于大密集矩阵)的情况下实现非平方矩阵的转置方法,而是在其中.还有办法吗?
转置矩阵的最有效方法是根本不转置它.
以允许通过另外存储行和列步骤和偏移来定义相同数据缓冲器上的子矩阵,切片或任何其他方式来设计矩阵类可能是最有效的.然后访问使用这些数据的元素来计算索引.要转置你只需要操纵这些步值.
您可以查看OpenCV的Matrix实现(仅用于实现功能,而不是用于类设计!)