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 case (n is the matrix dimension). For 0-index based, it would be.
i = index / n
j = index % n
Now, what if my matrix is symmetric and I only want to access the upper part
0 1 2 3
.. 4 5 6
..... 7 8
........ 9
I know the linear index will be given by
index = j + n*i - i (i-1) / 2
but I want to know i,j given idx. Do you guys know of any way of doing this?. I looked up this here, but I couldn't find an answer. Thanks in advance.
triu
function or do you really only need those indices as opposed to an upper triangular matrix? And why both c++ and Matlab tags? What are you actually looking for?ind2sub
. You can try that out.