3

I'm new to python and I have a question about numpy.reshape. I currently have 2 lists of values like this:

x = [0,1,2,3]
y = [4,5,6,7]

And I want them to be in separate 2D arrays, where each item is repeated for the length of the original lists, like this:

xx = [[0,0,0,0]
     [1,1,1,1]
     [2,2,2,2]
     [3,3,3,3]]

yy = [[4,5,6,7]
      [4,5,6,7]
      [4,5,6,7]
      [4,5,6,7]]

Is there a way to do this with numpy.reshape, or is there a better method I could use? I would very much appreciate a detailed explanation. Thanks!

1
  • 1
    This is exactly what np.meshgrid does Commented Jun 10, 2015 at 15:51

1 Answer 1

4

numpy.meshgrid will do this for you.

N.B. From your requested output, it looks like you want ij indexing, not the default xy

from numpy import meshgrid

x = [0,1,2,3]
y = [4,5,6,7]
xx,yy=meshgrid(x,y,indexing='ij')

print xx
>>> [[0 0 0 0]
     [1 1 1 1]
     [2 2 2 2]
     [3 3 3 3]]

print yy
>>> [[4 5 6 7]
     [4 5 6 7]
     [4 5 6 7]
     [4 5 6 7]]

For reference, here's xy indexing

xx,yy=meshgrid(x,y,indexing='xy')

print xx
>>> [[0 1 2 3]
     [0 1 2 3]
     [0 1 2 3]
     [0 1 2 3]]

print yy
>>> [[4 4 4 4]
     [5 5 5 5]
     [6 6 6 6]
     [7 7 7 7]]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.