flip numpy array along one dimension

Viewed 43


what's the numpy equivalent of the following function:

def customized_mirror(in_array):
    
    out_array = np.zeros((N, N, N))
    for idx in range(N):
        out_array[:, :, idx] = in_array[:, :, N-1-idx]

return out_array

I've tried all combinations of np.flip and did not get the expected, mirrored array.

1 Answers
out_array = in_array[:,:,::-1]

or

out_array = in_array[:,:,::-1].copy()

The first will cause out_array to contain the same actual data array as in_array, but with different indexing into the same data. The latter will copy it.

Related