Issue
I would like to slice the array so that the last value is followed by the first value.
Code :
import numpy as np
arr = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]]
m = np.array(arr)
print(m[-1:1, -1:1])
expected output :
[[16 13]
[4 1]]
actual output :
[]
Solution
You have to use advanced indexing:
In [64]: arr=np.arange(1,17).reshape(4,4)
In [65]: arr[[[3],[0]],[3,0]] # or -1 as in mozway's answer
Out[65]:
array([[16, 13],
[ 4, 1]])
On further thought, you can use a -3 step:
In [67]: arr[-1::-3, -1::-3]
Out[67]:
array([[16, 13],
[ 4, 1]])
The numbers and shape are the same, but strides
is different (one is a copy, the other a view (with somewhat funny strides):
In [69]: arr[[[-1],[0]],[-1,0]].strides
Out[69]: (8, 4)
In [70]: arr[-1::-3, -1::-3].strides
Out[70]: (-48, -12)
Answered By - hpaulj
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.