Reputation: 1013
I have an array of Cartesian coordinates produced from polars in a usual way:
for k in range(0, Phi_term):
for j in range(0, R_term):
X[k,j] = R[j]*np.cos(phi[k]);
Y[k,j] = R[j]*np.sin(phi[k]);
The problem is that the zeroth element of such an array corresponds to the origin of the polar circle. I would like to have an array of the same elements but starting in the top right corner. For example, elements in the current array distribute in the following way (for the upper half):
11 10 9 6 7 8
14 13 12 3 4 5
17 16 15 0 1 2
(imagine it's a circle). What I want to get is the grid starting with the zeroth element:
0 1 2 3 4 5
6 7 8 9 10 11
12 13 14 15 16 17
though preserving the values, i.e. the value of the 11th element of the initial array is now the value of the 0th element of the new array.
Is there any smart way to perform such a transformation in numpy
?
Upvotes: 0
Views: 933
Reputation: 20141
def quasiCartesianOrder(arr, R_term, Phi_term):
# deal with odd phi count by starting from top of top spike.
rhsOddOffset = 0
if Phi_term % 2 == 1:
rhsOddOffset = R_term
for r in xrange(0, R_term):
yield (Phi_term + 1)/2 * R_term - r - 1
# 'rectangular' section, starting down 11 o'clock and up 1 o'clock.
phiBaseLeft = Phi_term / 2 + rhsOddOffset/R_term
phiBaseRight = Phi_term / 2
for phiLine in xrange(0, Phi_term / 2):
# down 11
base = (phiBaseLeft + phiLine) * R_term - 1
for idx in xrange(base + R_term, base, -1):
yield idx
# up 1
base = (phiBaseRight - phiLine ) * R_term
for idx in xrange(base - R_term, base):
yield idx
Behaviour:
11
10
9
14 13 12 6 7 8
17 16 15 3 4 5
20 19 18 0 1 2
Becomes
0
1
2
3 4 5 6 7 8
9 10 11 12 13 14
15 16 17 18 19 20
Result
11 10 9 14 13 12 6 7 8 17 16 15 3 4 5 20 19 18 0 1 2
The style is a generator, so that you can iterate. If you just want the indices themselves, call list
with the returned generator, and you should be able to use that with numpy's index arrays stuff.
Upvotes: 1