numpy.ravel_multi_index() function | Python

numpy.ravel_multi_index() function converts a tuple of index arrays into an array of flat indices, applying boundary modes to the multi-index.

Syntax : numpy.ravel_multi_index(multi_index, dims, mode = ‘raise’, order = ‘C)
Parameters :
multi_index : [tuple of array_like] A tuple of integer arrays, one array for each dimension.
dims : [tuple of ints] The shape of array into which the indices from multi_index apply.
mode : [{‘raise’, ‘wrap’, ‘clip’}, optional] Specifies how out-of-bounds indices are handled. Can specify either one mode or a tuple of modes, one mode per index.
‘raise’ – raise an error (default)
‘wrap’ – wrap around
‘clip’ – clip to the range
In ‘clip’ mode, a negative index that would normally wrap will clip to 0 instead.
order : [{‘C’, ‘F’}, optional] Determines whether the multi-index should be viewed as indexing in row-major (C-style) or column-major (Fortran-style) order.

Return : [ndarray] An array of indices into the flattened version of an array of dimensions dims.

Code #1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program explaining
# numpy.ravel_multi_index() function
  
# importing numpy as geek 
import numpy as geek
  
arr = geek.array([[3, 6, 6], [4, 5, 1]])
  
gfg = geek.ravel_multi_index(arr, (7, 6))
  
print(gfg) 

chevron_right


Output :



[22 41 37]

 
Code #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program explaining
# numpy.ravel_multi_index() function
  
# importing numpy as geek 
import numpy as geek
  
arr = geek.array([[3, 6, 6], [4, 5, 1]])
  
gfg = geek.ravel_multi_index(arr, (7, 6), order = 'F')
  
print(gfg) 

chevron_right


Output :

[31 41 13]

 
Code #3 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program explaining
# numpy.ravel_multi_index() function
  
# importing numpy as geek 
import numpy as geek
  
arr = geek.array([[3, 6, 6], [4, 5, 1]])
  
gfg = geek.ravel_multi_index(arr, (7, 6), mode = 'clip')
  
print(gfg) 

chevron_right


Output :

[22 41 37]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.