Matrix in which rows are coordinates of points of a meshgrid












0















In this tutorial I find code like



import numpy as np
x = np.linspace(-5, 5, 20)
y = np.linspace(-1, 1, 10)
Y, X = np.meshgrid(y, x)
xy = np.vstack([X.ravel(), Y.ravel()]).T
print(xy)


Isn't there a shorter way to get a matrix where rows are coordinates of points of the meshgrid, than using meshgrid+vstack+ravel+transpose?



Output:



[[-5.         -1.        ]
[-5. -0.77777778]
[-5. -0.55555556]
[-5. -0.33333333]
[-5. -0.11111111]
[-5. 0.11111111]
[-5. 0.33333333]
[-5. 0.55555556]
[-5. 0.77777778]
[-5. 1. ]
[-4.47368421 -1. ]
[-4.47368421 -0.77777778]
...









share|improve this question























  • Numpy.repeat() for x and numpy.tile() for y coordinate.

    – Ante
    Nov 23 '18 at 8:10











  • @ante can you post it as an answer with code example?

    – Basj
    Nov 23 '18 at 9:43











  • @Bajs I posted answer with a code.

    – Ante
    Nov 23 '18 at 11:20
















0















In this tutorial I find code like



import numpy as np
x = np.linspace(-5, 5, 20)
y = np.linspace(-1, 1, 10)
Y, X = np.meshgrid(y, x)
xy = np.vstack([X.ravel(), Y.ravel()]).T
print(xy)


Isn't there a shorter way to get a matrix where rows are coordinates of points of the meshgrid, than using meshgrid+vstack+ravel+transpose?



Output:



[[-5.         -1.        ]
[-5. -0.77777778]
[-5. -0.55555556]
[-5. -0.33333333]
[-5. -0.11111111]
[-5. 0.11111111]
[-5. 0.33333333]
[-5. 0.55555556]
[-5. 0.77777778]
[-5. 1. ]
[-4.47368421 -1. ]
[-4.47368421 -0.77777778]
...









share|improve this question























  • Numpy.repeat() for x and numpy.tile() for y coordinate.

    – Ante
    Nov 23 '18 at 8:10











  • @ante can you post it as an answer with code example?

    – Basj
    Nov 23 '18 at 9:43











  • @Bajs I posted answer with a code.

    – Ante
    Nov 23 '18 at 11:20














0












0








0








In this tutorial I find code like



import numpy as np
x = np.linspace(-5, 5, 20)
y = np.linspace(-1, 1, 10)
Y, X = np.meshgrid(y, x)
xy = np.vstack([X.ravel(), Y.ravel()]).T
print(xy)


Isn't there a shorter way to get a matrix where rows are coordinates of points of the meshgrid, than using meshgrid+vstack+ravel+transpose?



Output:



[[-5.         -1.        ]
[-5. -0.77777778]
[-5. -0.55555556]
[-5. -0.33333333]
[-5. -0.11111111]
[-5. 0.11111111]
[-5. 0.33333333]
[-5. 0.55555556]
[-5. 0.77777778]
[-5. 1. ]
[-4.47368421 -1. ]
[-4.47368421 -0.77777778]
...









share|improve this question














In this tutorial I find code like



import numpy as np
x = np.linspace(-5, 5, 20)
y = np.linspace(-1, 1, 10)
Y, X = np.meshgrid(y, x)
xy = np.vstack([X.ravel(), Y.ravel()]).T
print(xy)


Isn't there a shorter way to get a matrix where rows are coordinates of points of the meshgrid, than using meshgrid+vstack+ravel+transpose?



Output:



[[-5.         -1.        ]
[-5. -0.77777778]
[-5. -0.55555556]
[-5. -0.33333333]
[-5. -0.11111111]
[-5. 0.11111111]
[-5. 0.33333333]
[-5. 0.55555556]
[-5. 0.77777778]
[-5. 1. ]
[-4.47368421 -1. ]
[-4.47368421 -0.77777778]
...






python arrays numpy mesh






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 8:47









BasjBasj

6,18632107233




6,18632107233













  • Numpy.repeat() for x and numpy.tile() for y coordinate.

    – Ante
    Nov 23 '18 at 8:10











  • @ante can you post it as an answer with code example?

    – Basj
    Nov 23 '18 at 9:43











  • @Bajs I posted answer with a code.

    – Ante
    Nov 23 '18 at 11:20



















  • Numpy.repeat() for x and numpy.tile() for y coordinate.

    – Ante
    Nov 23 '18 at 8:10











  • @ante can you post it as an answer with code example?

    – Basj
    Nov 23 '18 at 9:43











  • @Bajs I posted answer with a code.

    – Ante
    Nov 23 '18 at 11:20

















Numpy.repeat() for x and numpy.tile() for y coordinate.

– Ante
Nov 23 '18 at 8:10





Numpy.repeat() for x and numpy.tile() for y coordinate.

– Ante
Nov 23 '18 at 8:10













@ante can you post it as an answer with code example?

– Basj
Nov 23 '18 at 9:43





@ante can you post it as an answer with code example?

– Basj
Nov 23 '18 at 9:43













@Bajs I posted answer with a code.

– Ante
Nov 23 '18 at 11:20





@Bajs I posted answer with a code.

– Ante
Nov 23 '18 at 11:20












2 Answers
2






active

oldest

votes


















2














You can skip meshgrid and get what you want more directly by just taking the cartesian product of x and y:



from itertools import product
import numpy as np

x = np.linspace(-5, 5, 20)
y = np.linspace(-1, 1, 10)
xy = np.array(list(product(x,y)))
print(xy)


Output:



[[-5.         -1.        ]
[-5. -0.77777778]
[-5. -0.55555556]
[-5. -0.33333333]
[-5. -0.11111111]
[-5. 0.11111111]
[-5. 0.33333333]
[-5. 0.55555556]
[-5. 0.77777778]
[-5. 1. ]
[-4.47368421 -1. ]
[-4.47368421 -0.77777778]
[-4.47368421 -0.55555556]
[-4.47368421 -0.33333333]
...
]





share|improve this answer































    1














    Here is implementation with repeat and tile methods.



    import numpy as np

    x = np.linspace(-5, 5, 20)
    y = np.linspace(-1, 1, 10)
    xy = np.empty((len(x) * len(y), 2))
    xy[:, 0] = np.repeat(x, len(y))
    xy[:, 1] = np.tile(y, len(x))
    print(xy)


    I suppose meshgrid method uses same approach to fill it's result matrix.






    share|improve this answer
























    • For future reference: docs.scipy.org/doc/numpy-1.15.0/reference/generated/…

      – Basj
      Nov 23 '18 at 12:54











    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',
    autoActivateHeartbeat: false,
    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%2f53426968%2fmatrix-in-which-rows-are-coordinates-of-points-of-a-meshgrid%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









    2














    You can skip meshgrid and get what you want more directly by just taking the cartesian product of x and y:



    from itertools import product
    import numpy as np

    x = np.linspace(-5, 5, 20)
    y = np.linspace(-1, 1, 10)
    xy = np.array(list(product(x,y)))
    print(xy)


    Output:



    [[-5.         -1.        ]
    [-5. -0.77777778]
    [-5. -0.55555556]
    [-5. -0.33333333]
    [-5. -0.11111111]
    [-5. 0.11111111]
    [-5. 0.33333333]
    [-5. 0.55555556]
    [-5. 0.77777778]
    [-5. 1. ]
    [-4.47368421 -1. ]
    [-4.47368421 -0.77777778]
    [-4.47368421 -0.55555556]
    [-4.47368421 -0.33333333]
    ...
    ]





    share|improve this answer




























      2














      You can skip meshgrid and get what you want more directly by just taking the cartesian product of x and y:



      from itertools import product
      import numpy as np

      x = np.linspace(-5, 5, 20)
      y = np.linspace(-1, 1, 10)
      xy = np.array(list(product(x,y)))
      print(xy)


      Output:



      [[-5.         -1.        ]
      [-5. -0.77777778]
      [-5. -0.55555556]
      [-5. -0.33333333]
      [-5. -0.11111111]
      [-5. 0.11111111]
      [-5. 0.33333333]
      [-5. 0.55555556]
      [-5. 0.77777778]
      [-5. 1. ]
      [-4.47368421 -1. ]
      [-4.47368421 -0.77777778]
      [-4.47368421 -0.55555556]
      [-4.47368421 -0.33333333]
      ...
      ]





      share|improve this answer


























        2












        2








        2







        You can skip meshgrid and get what you want more directly by just taking the cartesian product of x and y:



        from itertools import product
        import numpy as np

        x = np.linspace(-5, 5, 20)
        y = np.linspace(-1, 1, 10)
        xy = np.array(list(product(x,y)))
        print(xy)


        Output:



        [[-5.         -1.        ]
        [-5. -0.77777778]
        [-5. -0.55555556]
        [-5. -0.33333333]
        [-5. -0.11111111]
        [-5. 0.11111111]
        [-5. 0.33333333]
        [-5. 0.55555556]
        [-5. 0.77777778]
        [-5. 1. ]
        [-4.47368421 -1. ]
        [-4.47368421 -0.77777778]
        [-4.47368421 -0.55555556]
        [-4.47368421 -0.33333333]
        ...
        ]





        share|improve this answer













        You can skip meshgrid and get what you want more directly by just taking the cartesian product of x and y:



        from itertools import product
        import numpy as np

        x = np.linspace(-5, 5, 20)
        y = np.linspace(-1, 1, 10)
        xy = np.array(list(product(x,y)))
        print(xy)


        Output:



        [[-5.         -1.        ]
        [-5. -0.77777778]
        [-5. -0.55555556]
        [-5. -0.33333333]
        [-5. -0.11111111]
        [-5. 0.11111111]
        [-5. 0.33333333]
        [-5. 0.55555556]
        [-5. 0.77777778]
        [-5. 1. ]
        [-4.47368421 -1. ]
        [-4.47368421 -0.77777778]
        [-4.47368421 -0.55555556]
        [-4.47368421 -0.33333333]
        ...
        ]






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 9:51









        teltel

        7,41621431




        7,41621431

























            1














            Here is implementation with repeat and tile methods.



            import numpy as np

            x = np.linspace(-5, 5, 20)
            y = np.linspace(-1, 1, 10)
            xy = np.empty((len(x) * len(y), 2))
            xy[:, 0] = np.repeat(x, len(y))
            xy[:, 1] = np.tile(y, len(x))
            print(xy)


            I suppose meshgrid method uses same approach to fill it's result matrix.






            share|improve this answer
























            • For future reference: docs.scipy.org/doc/numpy-1.15.0/reference/generated/…

              – Basj
              Nov 23 '18 at 12:54
















            1














            Here is implementation with repeat and tile methods.



            import numpy as np

            x = np.linspace(-5, 5, 20)
            y = np.linspace(-1, 1, 10)
            xy = np.empty((len(x) * len(y), 2))
            xy[:, 0] = np.repeat(x, len(y))
            xy[:, 1] = np.tile(y, len(x))
            print(xy)


            I suppose meshgrid method uses same approach to fill it's result matrix.






            share|improve this answer
























            • For future reference: docs.scipy.org/doc/numpy-1.15.0/reference/generated/…

              – Basj
              Nov 23 '18 at 12:54














            1












            1








            1







            Here is implementation with repeat and tile methods.



            import numpy as np

            x = np.linspace(-5, 5, 20)
            y = np.linspace(-1, 1, 10)
            xy = np.empty((len(x) * len(y), 2))
            xy[:, 0] = np.repeat(x, len(y))
            xy[:, 1] = np.tile(y, len(x))
            print(xy)


            I suppose meshgrid method uses same approach to fill it's result matrix.






            share|improve this answer













            Here is implementation with repeat and tile methods.



            import numpy as np

            x = np.linspace(-5, 5, 20)
            y = np.linspace(-1, 1, 10)
            xy = np.empty((len(x) * len(y), 2))
            xy[:, 0] = np.repeat(x, len(y))
            xy[:, 1] = np.tile(y, len(x))
            print(xy)


            I suppose meshgrid method uses same approach to fill it's result matrix.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 23 '18 at 10:28









            AnteAnte

            4,39451942




            4,39451942













            • For future reference: docs.scipy.org/doc/numpy-1.15.0/reference/generated/…

              – Basj
              Nov 23 '18 at 12:54



















            • For future reference: docs.scipy.org/doc/numpy-1.15.0/reference/generated/…

              – Basj
              Nov 23 '18 at 12:54

















            For future reference: docs.scipy.org/doc/numpy-1.15.0/reference/generated/…

            – Basj
            Nov 23 '18 at 12:54





            For future reference: docs.scipy.org/doc/numpy-1.15.0/reference/generated/…

            – Basj
            Nov 23 '18 at 12:54


















            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426968%2fmatrix-in-which-rows-are-coordinates-of-points-of-a-meshgrid%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

            The term 'EXEC' is not recognized as the name of a cmdlet Powershell

            NPM command prompt closes immediately [closed]

            Error binding properties and functions in emscripten