Issue
Lets say I have an array
a = np.arange(16).reshape((4,4))
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
But I want
15 11 7 3
14 10 6 2
13 9 5 1
12 8 4 0
which is a flip across the secondary diagonal, or a kind of anti-transpose.
How can I do this in numpy?
Solution
One could do one of the following:
rot90(a,2).T
rot90(flipud(a),1)
rot90(fliplr(a), -1)
or as hpaulj suggested in the comments (thanks hpaulj)
a[::-1,::-1].T
Here are the speed rankings as ratios of the slowest method after anti-transposing 1000 random 10000x10000 arrays.
- 63.5% -
a[::-1,::-1].T
- 85.6% -
rot90(a,2).T
- 97.8% -
rot90(flipud(a),1)
- 100% -
rot90(fliplr(a),-1)
Answered By - Jaden Travnik
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.