【问题标题】:Building a submatrix from a matrix从矩阵构建子矩阵
【发布时间】:2021-09-05 13:45:18
【问题描述】:

我正在尝试将给定的矩阵拆分为 4,但我遇到了一些错误,我不知道为什么。我对这门语言有点陌生。有谁知道我做错了什么?

void split_tl(T **matrice, unsigned int dim){
    if(dim == 1){
        return;
    }

    T **tl = new T*[dim/4];
    for(unsigned int i = 0; i<dim/4;++i){
        tl[i] = new T[dim/4];
    }

    for(unsigned int i=0; i<dim;++i){
        for(unsigned int j=0; j<dim;j++){
            if((i<dim/2) && (j<dim/2)){
                tl[i][j] = matrice[i][j];
            } else{
                std::cout << "no ";
            }
        }
        std::cout << std::endl;
    }
}

在这个函数中,我试图获取矩阵的左上角。

int **matrice = new int*[2];

for(unsigned int i = 0; i<2;++i){
    matrice[i] = new int[2];
}

for(unsigned int i = 0; i<2;++i){
    for(unsigned int j = 0; j<2;++j){
        matrice[i][j] = i+j;
    }
}

这是我要发送的矩阵。它是一个 2x2 矩阵,仅用于测试目的。

这些是 Valgrind 的错误:

==133== Invalid read of size 8
==133== Invalid write of size 4
==133== Process terminating with default action of signal 11 (SIGSEGV)
==133== Access not within mapped region at address 0x0

【问题讨论】:

  • new T[dim/4]; 的意义何在? (为什么是/4?)
  • 我忘了对不起,暗淡是矩阵的一面。我除以 4 因为我将它分成 4 个子矩阵

标签: c++ matrix submatrix


【解决方案1】:

如果 dim 是矩阵的边,分配到四分之一矩阵应该是 dim/2。

在您正在使用的代码中:

if((i<dim/2) && (j<dim/2)){
  tl[i][j] = matrice[i][j];
}

这里tl可能会超出分配

【讨论】:

    【解决方案2】:

    你做错了什么?您正在分配内存并传递指针。 建立一个适当的矩阵类,例如(非常简化的版本):

    template <typename T, unsigned Rows, unsigned Cols>
    struct generic_matrix {
        
        using datatype = T;
        
        static constexpr unsigned rows = Rows;
        static constexpr unsigned cols = Cols;
        
        datatype data[rows][cols];
    
        constexpr datatype& operator()(unsigned row, unsigned col) noexcept
        { return data[row][col]; }
        
        constexpr const datatype& operator()(unsigned row, unsigned col) const noexcept
        { return data[row][col]; }
        
    };
    

    子矩阵

    /* Returns a submatrix of the matrix m, 
     * by deleting the row r and the column c.*/
    template <typename M>
    auto submatrix(const M& m, unsigned r, unsigned c) noexcept
    {
        generic_matrix<typename M::datatype, M::rows-1, M::cols-1> res;
        
        for (unsigned row = 0, i = 0; row < M::rows; ++row) {
            if (row == r) continue; //this row we do not want
            for (unsigned col = 0, j = 0; col < M::cols; ++col) {
                if (col == c) continue; //this col we do not want
                res(i,j) = m(row,col);
                ++j;
            }
            ++i;
        }
    
        return res;
    }
    

    打印

    template <typename M>
    void printmatrix(const M& m) noexcept
    {
        for (unsigned r=0; r < M::rows; ++r) {
            for (unsigned c=0; c < M::cols; ++c) {
                std::cout << m(r,c) << ' ';
            }
            std::cout << '\n';
        }
    }
    

    测试

    int main()
    {
        int n=0;
        generic_matrix<int, 3, 3> m;
        for (int r = 0; r < 3; ++r)
            for (int c = 0; c < 3; ++c)
                m(r,c) = ++n;
    
        printmatrix(m);
        std::cout << '\n';
    
        printmatrix(submatrix(m, 0, 0));
        std::cout << '\n';
    
        printmatrix(submatrix(m, 1, 1));
        std::cout << '\n';
    
        printmatrix(submatrix(m, 2, 2));
    
        return 0;
    }
    

    注意:这只是一个提示。如您所见,不需要分配或强制转换。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-14
      相关资源
      最近更新 更多