10

Lets say that i have the following array (note that there is a 1 in the [2,0] position and a 2 in the [3,4] position):

[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[1, 0, 0, 0, 0]
[0, 0, 0, 0, 2]
[0, 0, 0, 0, 0]

and I want to flip it along the diagonal efficiently such that:

[0, 0, 1, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 2, 0]

This does not work with fliplr or rot90 or flipud. Would like efficient answer rather than just an answer since unfortunately this is not being performed on matrices this small.

user202729
  • 3,358
  • 3
  • 25
  • 36
negfrequency
  • 1,801
  • 3
  • 18
  • 30

1 Answers1

16

Both np.rot90(np.fliplr(x)) and transposing the array solves this.

a = np.random.uniform(size=(5,5))
a.T == np.rot90(np.fliplr(a))
Jurgen Strydom
  • 3,540
  • 1
  • 23
  • 30
negfrequency
  • 1,801
  • 3
  • 18
  • 30