Linear indexing in symmetric matrices

前端 未结 8 1164
遇见更好的自我
遇见更好的自我 2021-01-05 04:09

We can access matrices using linear indexing, which follows this pattern

0 1 2

3 4 5

6 7 8

It\'s easy to get the i,j coordinates for this cas

相关标签:
8条回答
  • 2021-01-05 04:24

    You can use this

    idxs = find(triu(true(size(A)))');
    

    which is an update on the answer of Matt. B, because you want row-wise representation.

    0 讨论(0)
  • 2021-01-05 04:34

    This is comment to Keeran Brabazon answer. k = j + ni - i(i-1) /2 - this is equation from your post and it's wrong, the correct equation is k = j + (2*n -1 -i)*i/2. But we can't use it for finding i.

    Equation from your post could be used to find i(row index), but we can't substitue i into your equation and get j, therefore formula for j in your post is wrong, so the final result will be looks like this:

    i = floor( ( 2*n+1 - sqrt( (2n+1)*(2n+1) - 8*k ) ) / 2 ) ;(exactly like yours)

    j = k - (2*n-1- i)*i/2; (different from your version, and i'm getting it by substituting i into my equation)

    0 讨论(0)
提交回复
热议问题