Reputation: 803
I'm building some software that involves a meshgrid created using np.meshgrid
and the matrix indexing (that is, setting indexing = `ij`
). However, the function that I'm working on could potentially take meshgrids created using the cartesian indexing (that is, setting indexing = `xy`
). Is there a quick way to go from indexing = `xy`
to indexing = `ij`
). When it's just 2 dimensional, I can simply transpose. However, when I go up to 3D, 4D, etc., transposition wouldn't do the job.
Upvotes: 0
Views: 1172
Reputation: 879739
indexing
only affects the order of the first two axes. swapaxes(0, 1)
can be used to reverse the order:
In [106]: I,J,K,L = np.meshgrid([1,2,3],['A','B'],[10,20,30,40],['X','Y','Z'], indexing='ij')
In [107]: X,Y,Z,W = np.meshgrid([1,2,3],['A','B'],[10,20,30,40],['X','Y','Z'], indexing='xy')
In [108]: (I.swapaxes(0,1) == X).all()
Out[108]: True
In [109]: (J.swapaxes(0,1) == Y).all()
Out[109]: True
In [110]: (K.swapaxes(0,1) == Z).all()
Out[110]: True
In [111]: (L.swapaxes(0,1) == W).all()
Out[111]: True
swapaxes(0, 1)
converts xy
to ij
indexing too (of course):
In [112]: (X.swapaxes(0,1) == I).all()
Out[112]: True
Upvotes: 1