Creating a diagonal matrix from from a vector












1












$begingroup$


I try to create a function that create a diagonal matrix in which the diagonal are:



$qquad 1,a,a^2, cdots,a^n,1,a,a^2, cdots,a^n$



Here is my attempt:



nth[n_] := DiagonalMatrix[Table[a^{Mod[k, n]}, {k, 0, 2 n - 1}]];


For some reason and I do not know wh,y the diagonal matrix is just one vector. So it seems DiagonalMatrix does not work as I expect.



Any idea as to where the mistake is in the function I created?










share|improve this question











$endgroup$

















    1












    $begingroup$


    I try to create a function that create a diagonal matrix in which the diagonal are:



    $qquad 1,a,a^2, cdots,a^n,1,a,a^2, cdots,a^n$



    Here is my attempt:



    nth[n_] := DiagonalMatrix[Table[a^{Mod[k, n]}, {k, 0, 2 n - 1}]];


    For some reason and I do not know wh,y the diagonal matrix is just one vector. So it seems DiagonalMatrix does not work as I expect.



    Any idea as to where the mistake is in the function I created?










    share|improve this question











    $endgroup$















      1












      1








      1





      $begingroup$


      I try to create a function that create a diagonal matrix in which the diagonal are:



      $qquad 1,a,a^2, cdots,a^n,1,a,a^2, cdots,a^n$



      Here is my attempt:



      nth[n_] := DiagonalMatrix[Table[a^{Mod[k, n]}, {k, 0, 2 n - 1}]];


      For some reason and I do not know wh,y the diagonal matrix is just one vector. So it seems DiagonalMatrix does not work as I expect.



      Any idea as to where the mistake is in the function I created?










      share|improve this question











      $endgroup$




      I try to create a function that create a diagonal matrix in which the diagonal are:



      $qquad 1,a,a^2, cdots,a^n,1,a,a^2, cdots,a^n$



      Here is my attempt:



      nth[n_] := DiagonalMatrix[Table[a^{Mod[k, n]}, {k, 0, 2 n - 1}]];


      For some reason and I do not know wh,y the diagonal matrix is just one vector. So it seems DiagonalMatrix does not work as I expect.



      Any idea as to where the mistake is in the function I created?







      functions matrix table






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 21 mins ago









      m_goldberg

      85.9k872196




      85.9k872196










      asked 5 hours ago









      henryhenry

      1346




      1346






















          1 Answer
          1






          active

          oldest

          votes


















          3












          $begingroup$

          Remove the braces around Mod:



          ClearAll[nth]
          nth[n_] := DiagonalMatrix@Table[a^Mod[k, n], {k, 0, 2 n - 1}]

          nth[5] // MatrixForm // TeXForm



          $left(
          begin{array}{cccccccccc}
          1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \
          0 & a & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \
          0 & 0 & a^2 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \
          0 & 0 & 0 & a^3 & 0 & 0 & 0 & 0 & 0 & 0 \
          0 & 0 & 0 & 0 & a^4 & 0 & 0 & 0 & 0 & 0 \
          0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \
          0 & 0 & 0 & 0 & 0 & 0 & a & 0 & 0 & 0 \
          0 & 0 & 0 & 0 & 0 & 0 & 0 & a^2 & 0 & 0 \
          0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & a^3 & 0 \
          0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & a^4 \
          end{array}
          right)$




          Alternatively, you can use SparseArray:



          ClearAll[nth2]
          nth2[n_] := SparseArray[{k_, k_} :> a^Mod[k, n], {2 n - 1, 2 n - 1}]
          nth2[5] // MatrixForm // TeXForm



          same result




          or use Band in combination with SparseArray:



          ClearAll[nth3]
          nth3[n_] := SparseArray[Band[{1, 1}] -> Table[a^Mod[k, n], {k, 0, 2 n - 1}]]

          nth2[5] // MatrixForm // TeXForm



          same result







          share|improve this answer











          $endgroup$













          • $begingroup$
            thanks.. it work perfectly.
            $endgroup$
            – henry
            5 hours ago










          • $begingroup$
            which one more practical? I mean later i need to compute power of modified verson of this matrix. so it will be much more computation.
            $endgroup$
            – henry
            5 hours ago








          • 1




            $begingroup$
            @henry, SparseArray +Band is likely to be the faster than DiagonalMatrix.
            $endgroup$
            – kglr
            5 hours ago










          • $begingroup$
            final question. If i use MatrixForm as a part of the function. does have any effect of efficiency of the computation?
            $endgroup$
            – henry
            5 hours ago






          • 1




            $begingroup$
            @henry, you should use MatrixForm is only for displaying (MatrixForm acts as a "wrapper", which affects printing, but not evaluation. )
            $endgroup$
            – kglr
            5 hours ago











          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: "387"
          };
          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',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f191217%2fcreating-a-diagonal-matrix-from-from-a-vector%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3












          $begingroup$

          Remove the braces around Mod:



          ClearAll[nth]
          nth[n_] := DiagonalMatrix@Table[a^Mod[k, n], {k, 0, 2 n - 1}]

          nth[5] // MatrixForm // TeXForm



          $left(
          begin{array}{cccccccccc}
          1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \
          0 & a & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \
          0 & 0 & a^2 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \
          0 & 0 & 0 & a^3 & 0 & 0 & 0 & 0 & 0 & 0 \
          0 & 0 & 0 & 0 & a^4 & 0 & 0 & 0 & 0 & 0 \
          0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \
          0 & 0 & 0 & 0 & 0 & 0 & a & 0 & 0 & 0 \
          0 & 0 & 0 & 0 & 0 & 0 & 0 & a^2 & 0 & 0 \
          0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & a^3 & 0 \
          0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & a^4 \
          end{array}
          right)$




          Alternatively, you can use SparseArray:



          ClearAll[nth2]
          nth2[n_] := SparseArray[{k_, k_} :> a^Mod[k, n], {2 n - 1, 2 n - 1}]
          nth2[5] // MatrixForm // TeXForm



          same result




          or use Band in combination with SparseArray:



          ClearAll[nth3]
          nth3[n_] := SparseArray[Band[{1, 1}] -> Table[a^Mod[k, n], {k, 0, 2 n - 1}]]

          nth2[5] // MatrixForm // TeXForm



          same result







          share|improve this answer











          $endgroup$













          • $begingroup$
            thanks.. it work perfectly.
            $endgroup$
            – henry
            5 hours ago










          • $begingroup$
            which one more practical? I mean later i need to compute power of modified verson of this matrix. so it will be much more computation.
            $endgroup$
            – henry
            5 hours ago








          • 1




            $begingroup$
            @henry, SparseArray +Band is likely to be the faster than DiagonalMatrix.
            $endgroup$
            – kglr
            5 hours ago










          • $begingroup$
            final question. If i use MatrixForm as a part of the function. does have any effect of efficiency of the computation?
            $endgroup$
            – henry
            5 hours ago






          • 1




            $begingroup$
            @henry, you should use MatrixForm is only for displaying (MatrixForm acts as a "wrapper", which affects printing, but not evaluation. )
            $endgroup$
            – kglr
            5 hours ago
















          3












          $begingroup$

          Remove the braces around Mod:



          ClearAll[nth]
          nth[n_] := DiagonalMatrix@Table[a^Mod[k, n], {k, 0, 2 n - 1}]

          nth[5] // MatrixForm // TeXForm



          $left(
          begin{array}{cccccccccc}
          1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \
          0 & a & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \
          0 & 0 & a^2 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \
          0 & 0 & 0 & a^3 & 0 & 0 & 0 & 0 & 0 & 0 \
          0 & 0 & 0 & 0 & a^4 & 0 & 0 & 0 & 0 & 0 \
          0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \
          0 & 0 & 0 & 0 & 0 & 0 & a & 0 & 0 & 0 \
          0 & 0 & 0 & 0 & 0 & 0 & 0 & a^2 & 0 & 0 \
          0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & a^3 & 0 \
          0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & a^4 \
          end{array}
          right)$




          Alternatively, you can use SparseArray:



          ClearAll[nth2]
          nth2[n_] := SparseArray[{k_, k_} :> a^Mod[k, n], {2 n - 1, 2 n - 1}]
          nth2[5] // MatrixForm // TeXForm



          same result




          or use Band in combination with SparseArray:



          ClearAll[nth3]
          nth3[n_] := SparseArray[Band[{1, 1}] -> Table[a^Mod[k, n], {k, 0, 2 n - 1}]]

          nth2[5] // MatrixForm // TeXForm



          same result







          share|improve this answer











          $endgroup$













          • $begingroup$
            thanks.. it work perfectly.
            $endgroup$
            – henry
            5 hours ago










          • $begingroup$
            which one more practical? I mean later i need to compute power of modified verson of this matrix. so it will be much more computation.
            $endgroup$
            – henry
            5 hours ago








          • 1




            $begingroup$
            @henry, SparseArray +Band is likely to be the faster than DiagonalMatrix.
            $endgroup$
            – kglr
            5 hours ago










          • $begingroup$
            final question. If i use MatrixForm as a part of the function. does have any effect of efficiency of the computation?
            $endgroup$
            – henry
            5 hours ago






          • 1




            $begingroup$
            @henry, you should use MatrixForm is only for displaying (MatrixForm acts as a "wrapper", which affects printing, but not evaluation. )
            $endgroup$
            – kglr
            5 hours ago














          3












          3








          3





          $begingroup$

          Remove the braces around Mod:



          ClearAll[nth]
          nth[n_] := DiagonalMatrix@Table[a^Mod[k, n], {k, 0, 2 n - 1}]

          nth[5] // MatrixForm // TeXForm



          $left(
          begin{array}{cccccccccc}
          1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \
          0 & a & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \
          0 & 0 & a^2 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \
          0 & 0 & 0 & a^3 & 0 & 0 & 0 & 0 & 0 & 0 \
          0 & 0 & 0 & 0 & a^4 & 0 & 0 & 0 & 0 & 0 \
          0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \
          0 & 0 & 0 & 0 & 0 & 0 & a & 0 & 0 & 0 \
          0 & 0 & 0 & 0 & 0 & 0 & 0 & a^2 & 0 & 0 \
          0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & a^3 & 0 \
          0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & a^4 \
          end{array}
          right)$




          Alternatively, you can use SparseArray:



          ClearAll[nth2]
          nth2[n_] := SparseArray[{k_, k_} :> a^Mod[k, n], {2 n - 1, 2 n - 1}]
          nth2[5] // MatrixForm // TeXForm



          same result




          or use Band in combination with SparseArray:



          ClearAll[nth3]
          nth3[n_] := SparseArray[Band[{1, 1}] -> Table[a^Mod[k, n], {k, 0, 2 n - 1}]]

          nth2[5] // MatrixForm // TeXForm



          same result







          share|improve this answer











          $endgroup$



          Remove the braces around Mod:



          ClearAll[nth]
          nth[n_] := DiagonalMatrix@Table[a^Mod[k, n], {k, 0, 2 n - 1}]

          nth[5] // MatrixForm // TeXForm



          $left(
          begin{array}{cccccccccc}
          1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \
          0 & a & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \
          0 & 0 & a^2 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \
          0 & 0 & 0 & a^3 & 0 & 0 & 0 & 0 & 0 & 0 \
          0 & 0 & 0 & 0 & a^4 & 0 & 0 & 0 & 0 & 0 \
          0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \
          0 & 0 & 0 & 0 & 0 & 0 & a & 0 & 0 & 0 \
          0 & 0 & 0 & 0 & 0 & 0 & 0 & a^2 & 0 & 0 \
          0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & a^3 & 0 \
          0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & a^4 \
          end{array}
          right)$




          Alternatively, you can use SparseArray:



          ClearAll[nth2]
          nth2[n_] := SparseArray[{k_, k_} :> a^Mod[k, n], {2 n - 1, 2 n - 1}]
          nth2[5] // MatrixForm // TeXForm



          same result




          or use Band in combination with SparseArray:



          ClearAll[nth3]
          nth3[n_] := SparseArray[Band[{1, 1}] -> Table[a^Mod[k, n], {k, 0, 2 n - 1}]]

          nth2[5] // MatrixForm // TeXForm



          same result








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 5 hours ago

























          answered 5 hours ago









          kglrkglr

          183k10201416




          183k10201416












          • $begingroup$
            thanks.. it work perfectly.
            $endgroup$
            – henry
            5 hours ago










          • $begingroup$
            which one more practical? I mean later i need to compute power of modified verson of this matrix. so it will be much more computation.
            $endgroup$
            – henry
            5 hours ago








          • 1




            $begingroup$
            @henry, SparseArray +Band is likely to be the faster than DiagonalMatrix.
            $endgroup$
            – kglr
            5 hours ago










          • $begingroup$
            final question. If i use MatrixForm as a part of the function. does have any effect of efficiency of the computation?
            $endgroup$
            – henry
            5 hours ago






          • 1




            $begingroup$
            @henry, you should use MatrixForm is only for displaying (MatrixForm acts as a "wrapper", which affects printing, but not evaluation. )
            $endgroup$
            – kglr
            5 hours ago


















          • $begingroup$
            thanks.. it work perfectly.
            $endgroup$
            – henry
            5 hours ago










          • $begingroup$
            which one more practical? I mean later i need to compute power of modified verson of this matrix. so it will be much more computation.
            $endgroup$
            – henry
            5 hours ago








          • 1




            $begingroup$
            @henry, SparseArray +Band is likely to be the faster than DiagonalMatrix.
            $endgroup$
            – kglr
            5 hours ago










          • $begingroup$
            final question. If i use MatrixForm as a part of the function. does have any effect of efficiency of the computation?
            $endgroup$
            – henry
            5 hours ago






          • 1




            $begingroup$
            @henry, you should use MatrixForm is only for displaying (MatrixForm acts as a "wrapper", which affects printing, but not evaluation. )
            $endgroup$
            – kglr
            5 hours ago
















          $begingroup$
          thanks.. it work perfectly.
          $endgroup$
          – henry
          5 hours ago




          $begingroup$
          thanks.. it work perfectly.
          $endgroup$
          – henry
          5 hours ago












          $begingroup$
          which one more practical? I mean later i need to compute power of modified verson of this matrix. so it will be much more computation.
          $endgroup$
          – henry
          5 hours ago






          $begingroup$
          which one more practical? I mean later i need to compute power of modified verson of this matrix. so it will be much more computation.
          $endgroup$
          – henry
          5 hours ago






          1




          1




          $begingroup$
          @henry, SparseArray +Band is likely to be the faster than DiagonalMatrix.
          $endgroup$
          – kglr
          5 hours ago




          $begingroup$
          @henry, SparseArray +Band is likely to be the faster than DiagonalMatrix.
          $endgroup$
          – kglr
          5 hours ago












          $begingroup$
          final question. If i use MatrixForm as a part of the function. does have any effect of efficiency of the computation?
          $endgroup$
          – henry
          5 hours ago




          $begingroup$
          final question. If i use MatrixForm as a part of the function. does have any effect of efficiency of the computation?
          $endgroup$
          – henry
          5 hours ago




          1




          1




          $begingroup$
          @henry, you should use MatrixForm is only for displaying (MatrixForm acts as a "wrapper", which affects printing, but not evaluation. )
          $endgroup$
          – kglr
          5 hours ago




          $begingroup$
          @henry, you should use MatrixForm is only for displaying (MatrixForm acts as a "wrapper", which affects printing, but not evaluation. )
          $endgroup$
          – kglr
          5 hours ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Mathematica Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f191217%2fcreating-a-diagonal-matrix-from-from-a-vector%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python