how to duplicate each row of a matrix N times Numpy









up vote
0
down vote

favorite
1












I have a matrix with these dimensions (150,2) and I want to duplicate each row N times. I show what I mean with an example.



Input:



a = [[2, 3], [5, 6], [7, 9]]


suppose N= 3, I want this output:



[[2 3]
[2 3]
[2 3]
[5 6]
[5 6]
[5 6]
[7 9]
[7 9]
[7 9]]


Thank you.










share|improve this question



















  • 2




    Can you please edit your sample data, right now it does not make much sense.
    – Willem Van Onsem
    Nov 10 at 13:07










  • I think your example a should be a = [[2, 3], [5, 6], [7, 9]].
    – Warren Weckesser
    Nov 10 at 13:09










  • I've posted a picture I hope will make it clear :) a is a column array
    – ggg
    Nov 10 at 13:10














up vote
0
down vote

favorite
1












I have a matrix with these dimensions (150,2) and I want to duplicate each row N times. I show what I mean with an example.



Input:



a = [[2, 3], [5, 6], [7, 9]]


suppose N= 3, I want this output:



[[2 3]
[2 3]
[2 3]
[5 6]
[5 6]
[5 6]
[7 9]
[7 9]
[7 9]]


Thank you.










share|improve this question



















  • 2




    Can you please edit your sample data, right now it does not make much sense.
    – Willem Van Onsem
    Nov 10 at 13:07










  • I think your example a should be a = [[2, 3], [5, 6], [7, 9]].
    – Warren Weckesser
    Nov 10 at 13:09










  • I've posted a picture I hope will make it clear :) a is a column array
    – ggg
    Nov 10 at 13:10












up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I have a matrix with these dimensions (150,2) and I want to duplicate each row N times. I show what I mean with an example.



Input:



a = [[2, 3], [5, 6], [7, 9]]


suppose N= 3, I want this output:



[[2 3]
[2 3]
[2 3]
[5 6]
[5 6]
[5 6]
[7 9]
[7 9]
[7 9]]


Thank you.










share|improve this question















I have a matrix with these dimensions (150,2) and I want to duplicate each row N times. I show what I mean with an example.



Input:



a = [[2, 3], [5, 6], [7, 9]]


suppose N= 3, I want this output:



[[2 3]
[2 3]
[2 3]
[5 6]
[5 6]
[5 6]
[7 9]
[7 9]
[7 9]]


Thank you.







python numpy matrix duplicates row






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 13:28









Sandeep Kadapa

5,519427




5,519427










asked Nov 10 at 13:05









ggg

134




134







  • 2




    Can you please edit your sample data, right now it does not make much sense.
    – Willem Van Onsem
    Nov 10 at 13:07










  • I think your example a should be a = [[2, 3], [5, 6], [7, 9]].
    – Warren Weckesser
    Nov 10 at 13:09










  • I've posted a picture I hope will make it clear :) a is a column array
    – ggg
    Nov 10 at 13:10












  • 2




    Can you please edit your sample data, right now it does not make much sense.
    – Willem Van Onsem
    Nov 10 at 13:07










  • I think your example a should be a = [[2, 3], [5, 6], [7, 9]].
    – Warren Weckesser
    Nov 10 at 13:09










  • I've posted a picture I hope will make it clear :) a is a column array
    – ggg
    Nov 10 at 13:10







2




2




Can you please edit your sample data, right now it does not make much sense.
– Willem Van Onsem
Nov 10 at 13:07




Can you please edit your sample data, right now it does not make much sense.
– Willem Van Onsem
Nov 10 at 13:07












I think your example a should be a = [[2, 3], [5, 6], [7, 9]].
– Warren Weckesser
Nov 10 at 13:09




I think your example a should be a = [[2, 3], [5, 6], [7, 9]].
– Warren Weckesser
Nov 10 at 13:09












I've posted a picture I hope will make it clear :) a is a column array
– ggg
Nov 10 at 13:10




I've posted a picture I hope will make it clear :) a is a column array
– ggg
Nov 10 at 13:10












2 Answers
2






active

oldest

votes

















up vote
4
down vote



accepted










Use np.repeat with parameter axis=0 as:



a = np.array([[2, 3],[5, 6],[7, 9]])

print(a)
[[2 3]
[5 6]
[7 9]]

r_a = np.repeat(a, repeats=3, axis=0)

print(r_a)
[[2 3]
[2 3]
[2 3]
[5 6]
[5 6]
[5 6]
[7 9]
[7 9]
[7 9]]





share|improve this answer



























    up vote
    0
    down vote













    To create an empty multidimensional array in NumPy (e.g. a 2D array m*n to store your matrix), in case you don't know m how many rows you will append and don't care about the computational cost Stephen Simmons mentioned (namely re-buildinging the array at each append), you can squeeze to 0 the dimension to which you want to append to: X = np.empty(shape=[0, n]).



    This way you can use for example (here m = 5 which we assume we didn't know when creating the empty matrix, and n = 2):



    import numpy as np

    n = 2
    X = np.empty(shape=[0, n])

    for i in range(5):
    for j in range(2):
    X = np.append(X, [[i, j]], axis=0)

    print X

    which will give you:

    [[ 0. 0.]
    [ 0. 1.]
    [ 1. 0.]
    [ 1. 1.]
    [ 2. 0.]
    [ 2. 1.]
    [ 3. 0.]
    [ 3. 1.]
    [ 4. 0.]
    [ 4. 1.]]





    share|improve this answer




















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      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: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      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%2fstackoverflow.com%2fquestions%2f53239242%2fhow-to-duplicate-each-row-of-a-matrix-n-times-numpy%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      4
      down vote



      accepted










      Use np.repeat with parameter axis=0 as:



      a = np.array([[2, 3],[5, 6],[7, 9]])

      print(a)
      [[2 3]
      [5 6]
      [7 9]]

      r_a = np.repeat(a, repeats=3, axis=0)

      print(r_a)
      [[2 3]
      [2 3]
      [2 3]
      [5 6]
      [5 6]
      [5 6]
      [7 9]
      [7 9]
      [7 9]]





      share|improve this answer
























        up vote
        4
        down vote



        accepted










        Use np.repeat with parameter axis=0 as:



        a = np.array([[2, 3],[5, 6],[7, 9]])

        print(a)
        [[2 3]
        [5 6]
        [7 9]]

        r_a = np.repeat(a, repeats=3, axis=0)

        print(r_a)
        [[2 3]
        [2 3]
        [2 3]
        [5 6]
        [5 6]
        [5 6]
        [7 9]
        [7 9]
        [7 9]]





        share|improve this answer






















          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          Use np.repeat with parameter axis=0 as:



          a = np.array([[2, 3],[5, 6],[7, 9]])

          print(a)
          [[2 3]
          [5 6]
          [7 9]]

          r_a = np.repeat(a, repeats=3, axis=0)

          print(r_a)
          [[2 3]
          [2 3]
          [2 3]
          [5 6]
          [5 6]
          [5 6]
          [7 9]
          [7 9]
          [7 9]]





          share|improve this answer












          Use np.repeat with parameter axis=0 as:



          a = np.array([[2, 3],[5, 6],[7, 9]])

          print(a)
          [[2 3]
          [5 6]
          [7 9]]

          r_a = np.repeat(a, repeats=3, axis=0)

          print(r_a)
          [[2 3]
          [2 3]
          [2 3]
          [5 6]
          [5 6]
          [5 6]
          [7 9]
          [7 9]
          [7 9]]






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 13:09









          Sandeep Kadapa

          5,519427




          5,519427






















              up vote
              0
              down vote













              To create an empty multidimensional array in NumPy (e.g. a 2D array m*n to store your matrix), in case you don't know m how many rows you will append and don't care about the computational cost Stephen Simmons mentioned (namely re-buildinging the array at each append), you can squeeze to 0 the dimension to which you want to append to: X = np.empty(shape=[0, n]).



              This way you can use for example (here m = 5 which we assume we didn't know when creating the empty matrix, and n = 2):



              import numpy as np

              n = 2
              X = np.empty(shape=[0, n])

              for i in range(5):
              for j in range(2):
              X = np.append(X, [[i, j]], axis=0)

              print X

              which will give you:

              [[ 0. 0.]
              [ 0. 1.]
              [ 1. 0.]
              [ 1. 1.]
              [ 2. 0.]
              [ 2. 1.]
              [ 3. 0.]
              [ 3. 1.]
              [ 4. 0.]
              [ 4. 1.]]





              share|improve this answer
























                up vote
                0
                down vote













                To create an empty multidimensional array in NumPy (e.g. a 2D array m*n to store your matrix), in case you don't know m how many rows you will append and don't care about the computational cost Stephen Simmons mentioned (namely re-buildinging the array at each append), you can squeeze to 0 the dimension to which you want to append to: X = np.empty(shape=[0, n]).



                This way you can use for example (here m = 5 which we assume we didn't know when creating the empty matrix, and n = 2):



                import numpy as np

                n = 2
                X = np.empty(shape=[0, n])

                for i in range(5):
                for j in range(2):
                X = np.append(X, [[i, j]], axis=0)

                print X

                which will give you:

                [[ 0. 0.]
                [ 0. 1.]
                [ 1. 0.]
                [ 1. 1.]
                [ 2. 0.]
                [ 2. 1.]
                [ 3. 0.]
                [ 3. 1.]
                [ 4. 0.]
                [ 4. 1.]]





                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  To create an empty multidimensional array in NumPy (e.g. a 2D array m*n to store your matrix), in case you don't know m how many rows you will append and don't care about the computational cost Stephen Simmons mentioned (namely re-buildinging the array at each append), you can squeeze to 0 the dimension to which you want to append to: X = np.empty(shape=[0, n]).



                  This way you can use for example (here m = 5 which we assume we didn't know when creating the empty matrix, and n = 2):



                  import numpy as np

                  n = 2
                  X = np.empty(shape=[0, n])

                  for i in range(5):
                  for j in range(2):
                  X = np.append(X, [[i, j]], axis=0)

                  print X

                  which will give you:

                  [[ 0. 0.]
                  [ 0. 1.]
                  [ 1. 0.]
                  [ 1. 1.]
                  [ 2. 0.]
                  [ 2. 1.]
                  [ 3. 0.]
                  [ 3. 1.]
                  [ 4. 0.]
                  [ 4. 1.]]





                  share|improve this answer












                  To create an empty multidimensional array in NumPy (e.g. a 2D array m*n to store your matrix), in case you don't know m how many rows you will append and don't care about the computational cost Stephen Simmons mentioned (namely re-buildinging the array at each append), you can squeeze to 0 the dimension to which you want to append to: X = np.empty(shape=[0, n]).



                  This way you can use for example (here m = 5 which we assume we didn't know when creating the empty matrix, and n = 2):



                  import numpy as np

                  n = 2
                  X = np.empty(shape=[0, n])

                  for i in range(5):
                  for j in range(2):
                  X = np.append(X, [[i, j]], axis=0)

                  print X

                  which will give you:

                  [[ 0. 0.]
                  [ 0. 1.]
                  [ 1. 0.]
                  [ 1. 1.]
                  [ 2. 0.]
                  [ 2. 1.]
                  [ 3. 0.]
                  [ 3. 1.]
                  [ 4. 0.]
                  [ 4. 1.]]






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 10 at 13:29









                  Mohammad reza Kashi

                  194




                  194



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • 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.

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





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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.

                      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%2fstackoverflow.com%2fquestions%2f53239242%2fhow-to-duplicate-each-row-of-a-matrix-n-times-numpy%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

                      How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                      Syphilis

                      Darth Vader #20