Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何有效地转置非平方矩阵?_C++_Performance_Matrix_Linear Algebra - Fatal编程技术网

C++ 如何有效地转置非平方矩阵?

C++ 如何有效地转置非平方矩阵?,c++,performance,matrix,linear-algebra,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) { fo

我创建了一个矩阵类,我想实现一个转置方法:

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
    }
}
模板
void矩阵::转置()
{
//关于方阵
如果(此->宽度()==此->高度())
{
对于(std::size_t y=1;yHeight();++y)
{
对于(标准::尺寸x=0;x

问题是如何实现非平方矩阵的转置方法,而无需分配一个全新的矩阵(该类用于大密度矩阵),而是就地分配。有什么办法吗?

转换矩阵最有效的方法是根本不转换矩阵

通过另外存储行和列步长以及偏移量,以允许在同一数据缓冲区上定义子矩阵、切片或任何内容的方式设计矩阵类可能是最有效的。 然后访问元素,使用这些数据计算索引。要进行转置,只需操纵这些步长值


您可以查看OpenCV的矩阵实现(只是为了实现功能,而不是为了类设计!)

非方矩阵的就地转置算法非常复杂:。还请注意,输出矩阵也需要交换其宽度和高度维度,因此您的类需要支持对这些维度的修改。可能与dup相关:如何将矩阵存储?@DDrmmr为1D
std::vector