Formula for converting a linear index to its mirrored counterpart on the other side of the diagonal in a symmetric matrix

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I’m looking for a formula which given a column-major linear index to an element in a symmetric $n times n$ matrix returns an index pointing to the corresponding location on the opposite side of the diagonal of the same matrix.
Say for example I have a matrix:
$$
A =
beginbmatrix
0 & 1 &2 \
1 & 0 & 2 \
2 & 2 & 0
endbmatrix
$$
And I enter $0$ into the formula, I would need it to return $0$ (since index $0$ is on the diagonal).



If I enter $1$ to the formula, I would need it to return $3$, since index $3$ points to the same location but mirrored to the opposite side of the diagonal.



If I enter $2$, it should return $6$. And so on...



This exact question has been asked before, but with row-major indexing instead of column-major. However, as far as I can tell, the accepted and upvoted answer on that tread is completely wrong so it offers little help.



Does anyone know how to solve this?







share|cite|improve this question


























    up vote
    0
    down vote

    favorite












    I’m looking for a formula which given a column-major linear index to an element in a symmetric $n times n$ matrix returns an index pointing to the corresponding location on the opposite side of the diagonal of the same matrix.
    Say for example I have a matrix:
    $$
    A =
    beginbmatrix
    0 & 1 &2 \
    1 & 0 & 2 \
    2 & 2 & 0
    endbmatrix
    $$
    And I enter $0$ into the formula, I would need it to return $0$ (since index $0$ is on the diagonal).



    If I enter $1$ to the formula, I would need it to return $3$, since index $3$ points to the same location but mirrored to the opposite side of the diagonal.



    If I enter $2$, it should return $6$. And so on...



    This exact question has been asked before, but with row-major indexing instead of column-major. However, as far as I can tell, the accepted and upvoted answer on that tread is completely wrong so it offers little help.



    Does anyone know how to solve this?







    share|cite|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I’m looking for a formula which given a column-major linear index to an element in a symmetric $n times n$ matrix returns an index pointing to the corresponding location on the opposite side of the diagonal of the same matrix.
      Say for example I have a matrix:
      $$
      A =
      beginbmatrix
      0 & 1 &2 \
      1 & 0 & 2 \
      2 & 2 & 0
      endbmatrix
      $$
      And I enter $0$ into the formula, I would need it to return $0$ (since index $0$ is on the diagonal).



      If I enter $1$ to the formula, I would need it to return $3$, since index $3$ points to the same location but mirrored to the opposite side of the diagonal.



      If I enter $2$, it should return $6$. And so on...



      This exact question has been asked before, but with row-major indexing instead of column-major. However, as far as I can tell, the accepted and upvoted answer on that tread is completely wrong so it offers little help.



      Does anyone know how to solve this?







      share|cite|improve this question














      I’m looking for a formula which given a column-major linear index to an element in a symmetric $n times n$ matrix returns an index pointing to the corresponding location on the opposite side of the diagonal of the same matrix.
      Say for example I have a matrix:
      $$
      A =
      beginbmatrix
      0 & 1 &2 \
      1 & 0 & 2 \
      2 & 2 & 0
      endbmatrix
      $$
      And I enter $0$ into the formula, I would need it to return $0$ (since index $0$ is on the diagonal).



      If I enter $1$ to the formula, I would need it to return $3$, since index $3$ points to the same location but mirrored to the opposite side of the diagonal.



      If I enter $2$, it should return $6$. And so on...



      This exact question has been asked before, but with row-major indexing instead of column-major. However, as far as I can tell, the accepted and upvoted answer on that tread is completely wrong so it offers little help.



      Does anyone know how to solve this?









      share|cite|improve this question













      share|cite|improve this question




      share|cite|improve this question








      edited Aug 20 at 10:36









      JayTuma

      1,333118




      1,333118










      asked Aug 20 at 9:41









      Petahanks

      31




      31




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          I take two steps to solve the problem:



          1) Find the matrix index in the form of $(i, j)$ corresponding to the number entered



          2) swap $(i, j)$ to $(j, i)$, which is for "mirroring". Find the index of the vector form of the column based matrix.



          The solution works for $1$ based, but can be converted easily to $0$ based since their difference for the same location is $1$.



          notation



          $leftlfloor fracln rightrfloor = l // n$



          Let $(i_l, j_l)$ be a mapping:
          beginequation
          labelvecmap
          beginsplit
          & (i_l, j_l): 1, ldots, n^2 to 1, ldots, ntimes 1, ldots, n\
          & l to (i_l, j_l)\
          & i_l = l - n(j_l-1) \
          & j_l = leftlfloor fracln rightrfloor \
          endsplit
          endequation



          For exmaple:
          $$
          beginsplit
          &l = 1, (i_1, j_1) = (1, 1)\
          &l = n, (i_l, j_l) = (n, 1)\
          &l = n+1, (i_l, j_l) = (1, 2)\
          &l = n^2, (i_l, j_l) = (n, n)\
          endsplit
          $$



          We want the "$l_*$" of $(j_l, i_l)$, which is $l_* = n(i_l - 1) + j_l$.



          Explanation
          Enter $i = 2$, the corresponding $l=3$ because I assume the starting index of series is 1 rather than 0. In your example, n = 3. Hence $i_l = 3, j_l = 1$, this gives $l_* = 3(3- 1) + 1 = 7$. Again because starting index is 1, converting by minus 1 you get 6.






          share|cite|improve this answer






















          • I’m afraid I don’t understand at all how what you have written solves the problem, if it does, could you please clarify how? To me it looks like you have just written a formula for subscript to linear indexing conversion which seems to have nothing to do with finding the mirrored index on the opposite of the diagonal. It also appears that you are working with 1-based indexing which is also incompatible with the question
            – Petahanks
            Aug 20 at 10:16











          • Please see explanation in the answer
            – kevin
            Aug 20 at 10:26










          • Ah! I get it now, it seems to work perfectly. Elegantly done, thank you!
            – Petahanks
            Aug 20 at 10:56










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "69"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          noCode: true, onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f2888590%2fformula-for-converting-a-linear-index-to-its-mirrored-counterpart-on-the-other-s%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          I take two steps to solve the problem:



          1) Find the matrix index in the form of $(i, j)$ corresponding to the number entered



          2) swap $(i, j)$ to $(j, i)$, which is for "mirroring". Find the index of the vector form of the column based matrix.



          The solution works for $1$ based, but can be converted easily to $0$ based since their difference for the same location is $1$.



          notation



          $leftlfloor fracln rightrfloor = l // n$



          Let $(i_l, j_l)$ be a mapping:
          beginequation
          labelvecmap
          beginsplit
          & (i_l, j_l): 1, ldots, n^2 to 1, ldots, ntimes 1, ldots, n\
          & l to (i_l, j_l)\
          & i_l = l - n(j_l-1) \
          & j_l = leftlfloor fracln rightrfloor \
          endsplit
          endequation



          For exmaple:
          $$
          beginsplit
          &l = 1, (i_1, j_1) = (1, 1)\
          &l = n, (i_l, j_l) = (n, 1)\
          &l = n+1, (i_l, j_l) = (1, 2)\
          &l = n^2, (i_l, j_l) = (n, n)\
          endsplit
          $$



          We want the "$l_*$" of $(j_l, i_l)$, which is $l_* = n(i_l - 1) + j_l$.



          Explanation
          Enter $i = 2$, the corresponding $l=3$ because I assume the starting index of series is 1 rather than 0. In your example, n = 3. Hence $i_l = 3, j_l = 1$, this gives $l_* = 3(3- 1) + 1 = 7$. Again because starting index is 1, converting by minus 1 you get 6.






          share|cite|improve this answer






















          • I’m afraid I don’t understand at all how what you have written solves the problem, if it does, could you please clarify how? To me it looks like you have just written a formula for subscript to linear indexing conversion which seems to have nothing to do with finding the mirrored index on the opposite of the diagonal. It also appears that you are working with 1-based indexing which is also incompatible with the question
            – Petahanks
            Aug 20 at 10:16











          • Please see explanation in the answer
            – kevin
            Aug 20 at 10:26










          • Ah! I get it now, it seems to work perfectly. Elegantly done, thank you!
            – Petahanks
            Aug 20 at 10:56














          up vote
          0
          down vote



          accepted










          I take two steps to solve the problem:



          1) Find the matrix index in the form of $(i, j)$ corresponding to the number entered



          2) swap $(i, j)$ to $(j, i)$, which is for "mirroring". Find the index of the vector form of the column based matrix.



          The solution works for $1$ based, but can be converted easily to $0$ based since their difference for the same location is $1$.



          notation



          $leftlfloor fracln rightrfloor = l // n$



          Let $(i_l, j_l)$ be a mapping:
          beginequation
          labelvecmap
          beginsplit
          & (i_l, j_l): 1, ldots, n^2 to 1, ldots, ntimes 1, ldots, n\
          & l to (i_l, j_l)\
          & i_l = l - n(j_l-1) \
          & j_l = leftlfloor fracln rightrfloor \
          endsplit
          endequation



          For exmaple:
          $$
          beginsplit
          &l = 1, (i_1, j_1) = (1, 1)\
          &l = n, (i_l, j_l) = (n, 1)\
          &l = n+1, (i_l, j_l) = (1, 2)\
          &l = n^2, (i_l, j_l) = (n, n)\
          endsplit
          $$



          We want the "$l_*$" of $(j_l, i_l)$, which is $l_* = n(i_l - 1) + j_l$.



          Explanation
          Enter $i = 2$, the corresponding $l=3$ because I assume the starting index of series is 1 rather than 0. In your example, n = 3. Hence $i_l = 3, j_l = 1$, this gives $l_* = 3(3- 1) + 1 = 7$. Again because starting index is 1, converting by minus 1 you get 6.






          share|cite|improve this answer






















          • I’m afraid I don’t understand at all how what you have written solves the problem, if it does, could you please clarify how? To me it looks like you have just written a formula for subscript to linear indexing conversion which seems to have nothing to do with finding the mirrored index on the opposite of the diagonal. It also appears that you are working with 1-based indexing which is also incompatible with the question
            – Petahanks
            Aug 20 at 10:16











          • Please see explanation in the answer
            – kevin
            Aug 20 at 10:26










          • Ah! I get it now, it seems to work perfectly. Elegantly done, thank you!
            – Petahanks
            Aug 20 at 10:56












          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          I take two steps to solve the problem:



          1) Find the matrix index in the form of $(i, j)$ corresponding to the number entered



          2) swap $(i, j)$ to $(j, i)$, which is for "mirroring". Find the index of the vector form of the column based matrix.



          The solution works for $1$ based, but can be converted easily to $0$ based since their difference for the same location is $1$.



          notation



          $leftlfloor fracln rightrfloor = l // n$



          Let $(i_l, j_l)$ be a mapping:
          beginequation
          labelvecmap
          beginsplit
          & (i_l, j_l): 1, ldots, n^2 to 1, ldots, ntimes 1, ldots, n\
          & l to (i_l, j_l)\
          & i_l = l - n(j_l-1) \
          & j_l = leftlfloor fracln rightrfloor \
          endsplit
          endequation



          For exmaple:
          $$
          beginsplit
          &l = 1, (i_1, j_1) = (1, 1)\
          &l = n, (i_l, j_l) = (n, 1)\
          &l = n+1, (i_l, j_l) = (1, 2)\
          &l = n^2, (i_l, j_l) = (n, n)\
          endsplit
          $$



          We want the "$l_*$" of $(j_l, i_l)$, which is $l_* = n(i_l - 1) + j_l$.



          Explanation
          Enter $i = 2$, the corresponding $l=3$ because I assume the starting index of series is 1 rather than 0. In your example, n = 3. Hence $i_l = 3, j_l = 1$, this gives $l_* = 3(3- 1) + 1 = 7$. Again because starting index is 1, converting by minus 1 you get 6.






          share|cite|improve this answer














          I take two steps to solve the problem:



          1) Find the matrix index in the form of $(i, j)$ corresponding to the number entered



          2) swap $(i, j)$ to $(j, i)$, which is for "mirroring". Find the index of the vector form of the column based matrix.



          The solution works for $1$ based, but can be converted easily to $0$ based since their difference for the same location is $1$.



          notation



          $leftlfloor fracln rightrfloor = l // n$



          Let $(i_l, j_l)$ be a mapping:
          beginequation
          labelvecmap
          beginsplit
          & (i_l, j_l): 1, ldots, n^2 to 1, ldots, ntimes 1, ldots, n\
          & l to (i_l, j_l)\
          & i_l = l - n(j_l-1) \
          & j_l = leftlfloor fracln rightrfloor \
          endsplit
          endequation



          For exmaple:
          $$
          beginsplit
          &l = 1, (i_1, j_1) = (1, 1)\
          &l = n, (i_l, j_l) = (n, 1)\
          &l = n+1, (i_l, j_l) = (1, 2)\
          &l = n^2, (i_l, j_l) = (n, n)\
          endsplit
          $$



          We want the "$l_*$" of $(j_l, i_l)$, which is $l_* = n(i_l - 1) + j_l$.



          Explanation
          Enter $i = 2$, the corresponding $l=3$ because I assume the starting index of series is 1 rather than 0. In your example, n = 3. Hence $i_l = 3, j_l = 1$, this gives $l_* = 3(3- 1) + 1 = 7$. Again because starting index is 1, converting by minus 1 you get 6.







          share|cite|improve this answer














          share|cite|improve this answer



          share|cite|improve this answer








          edited Aug 20 at 10:48

























          answered Aug 20 at 9:58









          kevin

          986




          986











          • I’m afraid I don’t understand at all how what you have written solves the problem, if it does, could you please clarify how? To me it looks like you have just written a formula for subscript to linear indexing conversion which seems to have nothing to do with finding the mirrored index on the opposite of the diagonal. It also appears that you are working with 1-based indexing which is also incompatible with the question
            – Petahanks
            Aug 20 at 10:16











          • Please see explanation in the answer
            – kevin
            Aug 20 at 10:26










          • Ah! I get it now, it seems to work perfectly. Elegantly done, thank you!
            – Petahanks
            Aug 20 at 10:56
















          • I’m afraid I don’t understand at all how what you have written solves the problem, if it does, could you please clarify how? To me it looks like you have just written a formula for subscript to linear indexing conversion which seems to have nothing to do with finding the mirrored index on the opposite of the diagonal. It also appears that you are working with 1-based indexing which is also incompatible with the question
            – Petahanks
            Aug 20 at 10:16











          • Please see explanation in the answer
            – kevin
            Aug 20 at 10:26










          • Ah! I get it now, it seems to work perfectly. Elegantly done, thank you!
            – Petahanks
            Aug 20 at 10:56















          I’m afraid I don’t understand at all how what you have written solves the problem, if it does, could you please clarify how? To me it looks like you have just written a formula for subscript to linear indexing conversion which seems to have nothing to do with finding the mirrored index on the opposite of the diagonal. It also appears that you are working with 1-based indexing which is also incompatible with the question
          – Petahanks
          Aug 20 at 10:16





          I’m afraid I don’t understand at all how what you have written solves the problem, if it does, could you please clarify how? To me it looks like you have just written a formula for subscript to linear indexing conversion which seems to have nothing to do with finding the mirrored index on the opposite of the diagonal. It also appears that you are working with 1-based indexing which is also incompatible with the question
          – Petahanks
          Aug 20 at 10:16













          Please see explanation in the answer
          – kevin
          Aug 20 at 10:26




          Please see explanation in the answer
          – kevin
          Aug 20 at 10:26












          Ah! I get it now, it seems to work perfectly. Elegantly done, thank you!
          – Petahanks
          Aug 20 at 10:56




          Ah! I get it now, it seems to work perfectly. Elegantly done, thank you!
          – Petahanks
          Aug 20 at 10:56












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f2888590%2fformula-for-converting-a-linear-index-to-its-mirrored-counterpart-on-the-other-s%23new-answer', 'question_page');

          );

          Post as a guest













































































          這個網誌中的熱門文章

          How to combine Bézier curves to a surface?

          Why am i infinitely getting the same tweet with the Twitter Search API?

          Is there any way to eliminate the singular point to solve this integral by hand or by approximations?