SimPEG.utils.get_subarray#

SimPEG.utils.get_subarray(A, ind)[source]#

Extract a subarray

For a numpy.ndarray, the function get_subarray extracts a subset of the array. The portion of the original array being extracted is defined by providing the indices along each axis.

Parameters
Anumpy.ndarray

The original numpy array. Must be 1, 2 or 3 dimensions.

ind(dim) list of numpy.ndarray

A list of numpy arrays containing the indices being extracted along each dimension. The length of the list must equal the dimensions of the input array.

Returns
numpy.ndarray

The subarray extracted from the original array

Examples

Here we construct a random 3x3 numpy array and use get_subarray to extract the first column.

>>> from discretize.utils import get_subarray
>>> import numpy as np
>>> A = np.random.rand(3, 3)
>>> A
array([[1.07969034e-04, 9.78613931e-01, 6.62123429e-01],
       [8.80722877e-01, 7.61035691e-01, 7.42546796e-01],
       [9.09488911e-01, 7.80626334e-01, 8.67663825e-01]])

Define the indexing along the columns and rows and create the indexing list

>>> ind_x = np.array([0, 1, 2])
>>> ind_y = np.array([0, 2])
>>> ind = [ind_x, ind_y]

Extract the first, and third column of A

>>> get_subarray(A, ind)
array([[1.07969034e-04, 6.62123429e-01],
       [8.80722877e-01, 7.42546796e-01],
       [9.09488911e-01, 8.67663825e-01]])