algorithm for index numbers of triangular matrix coefficients

前端 未结 7 721
广开言路
广开言路 2020-12-04 22:29

I think this must be simple but I can\'t get it right...

I have an MxM triangular matrix, the coefficients of which are stored in a vector, row by row. For example:

相关标签:
7条回答
  • 2020-12-04 23:01

    There may be a clever one liner for these, but (minus any error checking):

    unsigned int row_index( unsigned int i, unsigned int M ){
        unsigned int row = 0;
        unsigned int delta = M - 1;
        for( unsigned int x = delta; x < i; x += delta-- ){
            row++;
        }
        return row;
    }
    
    unsigned int column_index( unsigned int i, unsigned int M ){
        unsigned int row = 0;
        unsigned int delta = M - 1;
        unsigned int x;
        for( x = delta; x < i; x += delta-- ){
            row++;
        }
        return M + i - x - 1;
    }
    
    0 讨论(0)
提交回复
热议问题