numpy.unravel_index理解

np.unravel_index

参考:
http://blog.csdn.net/dn_mug/article/details/70256109
https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.unravel_index.html

numpy.unravel_index(indices, dims, order='C')

>>> np.unravel_index([22, 41, 37], (7,6))
(array([3, 6, 6]), array([4, 5, 1]))
>>> np.unravel_index([31, 41, 13], (7,6), order='F')
(array([3, 6, 6]), array([4, 5, 1]))
>>> np.unravel_index(1621, (6,7,8,9))
(3, 1, 4, 1)

功能:
假设将数组flatten,那么flattened的数组的索引为indices(可以是多个indice组成的数组)的元素,在原来的多维数组中的坐标是多少
dims是tuple,指定原多维数组的维度, order指示将x坐标还是y坐标放在前面,x对应的是col, y对应的是row.
通常坐标表示为(x, y)形式,但数组一般是row(即y)在前,col在后

举例子:
一个3x3的矩阵,flatten后,其索引对应图为

|0|1|2|
|3|4|5|
|6|7|8|

那么indice = 6,7,8 对应的原坐标:
(col在前, order参数为'F')为 (0,2) (1,2) (2,2)
(row在前,order参数为默认'C')为(2,0) , (2,1), (2,2)

np.unravel_index([6,7,8], (3,3))
(array([2, 2, 2]), array([0, 1, 2]))
2 2 2
0 1 2

In [23]: np.unravel_index([6], (3,3))
Out[23]: (array([2]), array([0]))

原创文章,转载请注明: 转载自张哲的博客

本文链接地址: numpy.unravel_index理解