Python numpy: reshape list into repeating 2D array -
i'm new python , have question numpy.reshape. have 2 lists of values this:
x = [0,1,2,3] y = [4,5,6,7]
and want them in separate 2d arrays, each item repeated length of original lists, 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 way numpy.reshape, or there better method use? appreciate detailed explanation. thanks!
numpy.meshgrid
you.
n.b. requested output, looks want ij
indexing, not 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]]
Comments
Post a Comment