Reputation: 16488
say I have three grids,
a = arange(0,5)
b = arange(0,3)
c = arange(10,12)
And for some reason, in my code, I first need to mesh
A, B = meshgrid(a,b,indexing='ij')
Is there a short way in which I could do
A, B, C = remeshgrid(A, B, c, indexing='ij)
such that A
, B
, C
all correspond to meshgrid(a,b,c, indexing='ij')?
The scenario is the following. - I first have a, b and mesh A, B - Later on, I generate c - At this point, I need to remesh everything containing c. But the code does not have a,b at disposal anymore.
So now it's a tradeoff of adjusting code in order to pass over a, b - if remeshing A,B is not possible or too inefficient.
Do you guys have any thoughts on this?
Upvotes: 0
Views: 130
Reputation: 879501
Building on Davidmh's idea, you could use A[:,0]
and B[0,:]
. This will work even if A
or B
contains duplicate values; and taking a slice is faster than calling np.unique
.
In [71]: A[:,0]
Out[71]: array([0, 1, 2, 3, 4])
In [72]: B[0,:]
Out[72]: array([0, 1, 2])
In [73]: A, B, C = np.meshgrid(A[:,0], B[0,:], c, indexing='ij')
Upvotes: 1
Reputation: 3865
You can reconstruct a
and b
taking the unique elements:
print A
array([[0, 0, 0],
[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4]])
print B
array([[0, 1, 2],
[0, 1, 2],
[0, 1, 2],
[0, 1, 2],
[0, 1, 2]])
np.unique(A)
array([0, 1, 2, 3, 4])
np.unique(B)
array([0, 1, 2])
Upvotes: 0