1

I try to rotate the matrix so that the first line is the last and the last is the first. And proceed in this way with the other lines in array

import numpy as np
m = np.array([[11, 12, 13, 14, 15],
              [21, 22, 23, 24, 25],
              [31, 32, 33, 34, 35],
              [41, 42, 43, 44, 45]])

My attempt:

p = np.rot90(m, 2)
array([[45, 44, 43, 42, 41],
       [35, 34, 33, 32, 31],
       [25, 24, 23, 22, 21],
       [15, 14, 13, 12, 11]])

I need

array([[41, 42, 43, 44, 45],
       [31, 32, 33, 34, 35],
       [21, 22, 23, 24, 25],
       [11, 12, 13, 14, 15]])

Do you have any advice?

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264

3 Answers3

1
np.flip(m,axis=0)

output

array([[41, 42, 43, 44, 45],
       [31, 32, 33, 34, 35],
       [21, 22, 23, 24, 25],
       [11, 12, 13, 14, 15]])
Rob Raymond
  • 29,118
  • 3
  • 14
  • 30
1

You're essentially trying to flip the matrix vertically and the function is literally flipud:

np.flipud(m)
Pacific Stickler
  • 1,078
  • 8
  • 20
0

Aside from flipud(m) and flip(m, axis=0), you can use simple indexing to reverse the first dimension:

m[::-1, :]

or even just

m[::-1]

If you really wanted arcane overkill, you could do something with np.lib.stride_tricks.as_strided:

np.lib.stride_tricks.as_strided(m[-1], shape=m.shape, strides=(-m.strides[0], m.strides[1]))
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264