What is an intuitive explanation of np.unravel_index?

后端 未结 5 1658
一生所求
一生所求 2020-12-13 04:04

Pretty much what the title says. I\'ve read the documentation and I\'ve played with the function for a while now but I can\'t discern what the physical manifestation of this

5条回答
  •  無奈伤痛
    2020-12-13 04:35

    This is only applicable for the 2D case, but the two coordinates np.unravel_index functions returns in this case are equivalent to doing floor division and applying the modulo function respectively.

    for j in range(1,1000):
        for i in range(j):
            assert(np.unravel_index(i,(987654321,j))==(i//j,i%j))
    

    The first element of the shape array (ie 987654321) is meaningless except to put an upper bound on how large an unraveled linear index can be passed through the function.

提交回复
热议问题