Function iaind2sub

namespace:ia636
page:iaind2sub

Synopse

Convert linear index to double subscripts. It is recommended to use the Numpy function unravel_index.

  • x, y = iaind2sub(dim, i)
    • x: Image.
    • y: Image.
    • dim: Image. Dimension.
    • i: Image. Index.
1. from numpy import *
2. 
3. def iaind2sub(dim, i):
4. 
5.     x = i / dim[1]
6.     y = i % dim[1]
7.     return x, y

Examples

Example 1

In this example, the first (0) and fifth (4) non zero elements of an bidimensional array are printed.

01. from ia636 import iaind2sub
02. 
03. f = array([[0,6,0,2],[4,0,1,8],[0,0,3,0]])
04. i = flatnonzero(f)
05. (x,y) = iaind2sub(f.shape, i)
06. print 'f=',f
07. print 'i=',i
08. print 'x=',x
09. print 'y=',y
10. print 'f[x[0],y[0]]=',f[x[0],y[0]]
11. print 'f[x[4],y[4]]=',f[x[4],y[4]]
f= [[0 6 0 2]
 [4 0 1 8]
 [0 0 3 0]]
i= [ 1  3  4  6  7 10]
x= [0 0 1 1 1 2]
y= [1 3 0 2 3 2]
f[x[0],y[0]]= 6
f[x[4],y[4]]= 8

See Also:

  • unravel_index - Numpy, convert linear index to multidimensional indexes