What produces unravel_index
?
In [126]: indx=np.array([2,4,6])
In [128]: i=np.unravel_index(indx,(3,3))
In [129]: i
Out[129]: (array([0, 1, 2]), array([2, 1, 0]))
A tuple of 2 indexing arrays. Apply this to the 2d array, and we see that it sets the opposite diagonal.
In [130]: a=np.zeros((3,3),int)
In [131]: a[i]=1
In [132]: a
Out[132]:
array([[0, 0, 1],
[0, 1, 0],
[1, 0, 0]])
2- 3D-? 3 , ?
In [133]: a=np.zeros((2,3,3),int)
In [134]: a[1,[0,1,2],[2,1,0]]=1
# a[(1,[0,1,2],[2,1,0])]=1 is the same thing
In [135]: a
Out[135]:
array([[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
[[0, 0, 1],
[0, 1, 0],
[1, 0, 0]]])
, : (array([0, 1, 2]), array([2, 1, 0]))
(1,[0,1,2],[2,1,0])
?
1- (1,3,3)
, 3- . (2,3,3)
.
In [140]: np.unravel_index(indx,(1,3,3))
Out[140]: (array([0, 0, 0]), array([0, 1, 2]), array([2, 1, 0]))
?
In [142]: (1,)+np.unravel_index(indx,(3,3))
Out[142]: (1, array([0, 1, 2]), array([2, 1, 0]))
numpy
, , , .
:
In [153]: ind=[1,0,0]
In [154]: ind[1:]=np.unravel_index(indx,(3,3))
In [155]: a[tuple(ind)]=2