Linear index upper triangular matrix

后端 未结 4 1239
滥情空心
滥情空心 2020-12-07 11:30

If I have the upper triangular portion of a matrix, offset above the diagonal, stored as a linear array, how can the (i,j) indices of a matrix element be extrac

4条回答
  •  天涯浪人
    2020-12-07 12:24

    The following is an implimentation in matlab, which can be easily transferred to another language, like C++. Here, we suppose the matrix has size m*m, ind is the index in the linear array. The only thing different is that here, we count the lower triangular part of the matrix column by column, which is analogus to your case (counting the upper triangular part row by row).

    function z= ind2lTra (ind, m)
      rvLinear = (m*(m-1))/2-ind;
      k = floor( (sqrt(1+8*rvLinear)-1)/2 );
    
      j= rvLinear - k*(k+1)/2;
    
      z=[m-j, m-(k+1)];
    

提交回复
热议问题