discretize.utils.sub2ind#

discretize.utils.sub2ind(shape, subs)[source]#

Return indices of tensor grid elements from subscripts.

This function is a wrapper for numpy.ravel_multi_index() with a hard-coded Fortran order, and a column order for the multi_index

Consider elements of a tensors grid whose positions are given by the subscripts (i,j,k). This function will return the corresponding indices of these elements. Each row of the input array subs defines the ijk for a particular tensor element.

Parameters:
shape(dim) tuple of int

Defines the shape of the tensor (1D, 2D or 3D).

subs(N, dim) array_like of int

The subscripts of the tensor grid elements. Each rows defines the position of a particular tensor element. The shape of of the array is (N, ndim).

Returns:
numpy.ndarray of int

The indices of the tensor grid elements defined by subs.

Examples

He we recreate the examples from numpy.ravel_multi_index() to illustrate the differences. The indices corresponding to each dimension are now columns in the array (instead of rows), and it assumed to use a Fortran order.

>>> import numpy as np
>>> from discretize.utils import sub2ind
>>> arr = np.array([[3, 4], [6, 5], [6, 1]])
>>> sub2ind((7, 6), arr)
array([31, 41, 13], dtype=int64)