Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flip 3D numpy Array

I need to flip 3D Array A shaped [m, n, k], along the Z axis. I need plane 0 (A[0,:,:]) to become k-1, plane 1 become k-2 plane and so on.

I need to do that on many array's, and loops are very slow.

I tried:

C = numpy.rot90(A,2)
C = flipud(A)
C = A[::-1]

I tried also rol and reshape, not what I needed.

For example: A is (3, 2, 2)

    A= np.array([[[ 1.01551435, -0.76494131],
 [ 0.56853752 , 1.94491724]],
[[-0.97433012 , 2.08134198],
 [-1.34997602 ,-0.33543117]],
[[ 0.54217072, -1.33470658],
 [-0.50179028, -0.66593918]]])

I need to reorder Z axis upside down:

[[ 0.54217072 -1.33470658]
 [-0.50179028 -0.66593918]]
[[-0.7703279   0.02402204]
 [-0.18006451 -0.37589744]]
[[ 1.01551435 -0.76494131]
 [ 0.56853752  1.94491724]]

Any ideas ?

like image 258
Naomi Fridman Avatar asked Oct 18 '22 02:10

Naomi Fridman


1 Answers

As @hpaulj suggested:

A = A[::-1, :, :]

print A.shape
print A

    (3L, 2L, 2L)

[[[ 0.54217072 -1.33470658]
  [-0.50179028 -0.66593918]]

 [[-0.97433012  2.08134198]
  [-1.34997602 -0.33543117]]

 [[ 1.01551435 -0.76494131]
  [ 0.56853752  1.94491724]]]
like image 162
Naomi Fridman Avatar answered Oct 21 '22 05:10

Naomi Fridman