make efficient the copy of symmetric matrix in c#

后端 未结 3 1734
伪装坚强ぢ
伪装坚强ぢ 2020-12-03 16:23

I want to store in an array a symmetric matrix

for a matrix I was doing this

    double[,] mat = new double[size,size];
    for (int i = 0; i < s         


        
3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-03 17:17

    Regarding the selected answer, unless I am being a complete idiot the code isn't correct:

    Try this:

    i = 2, j = 1
    
    therefore we use:
    
    k(i,j) = j*N-j*(j-1)/2+i
    
    to find the index k, solving:
    
    k(i,j) = 1*5 - 1*(1-1)/2 + 2
    
    k(i,j) = 5 - 0 + 2 = 7
    

    From the matrix in the selected answer we see that (2,1) is not 7, it seems to be 6. In fact (since this seems to be 0-base), 7 occurs at (3,1) or (1,3). The second formula for i > j seems to be inaccurate unless I am missing something.

    UPDATE:

    This seems to work if you alter the i > j formula to:

    k(i,j) = j*(N-1)-j*(j-1)/2+i
    

提交回复
热议问题