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?

1
  • 2
    Isn't it m[::-1,] Commented Mar 8, 2021 at 18:35

3 Answers 3

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]])
Sign up to request clarification or add additional context in comments.

Comments

1

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

np.flipud(m)

Comments

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]))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.