NumpyTensor.__getitem__

NumpyTensor.__getitem__(self, indices)[source]

Return self[indices].

Parameters
indicesindex expression

Integer, slice or sequence of these, defining the positions of the data array which should be accessed.

Returns
valuesNumpyTensorSpace.dtype or NumpyTensor

The value(s) at the given indices. Note that the returned object is a writable view into the original tensor, except for the case when indices is a list.

Examples

For one-dimensional spaces, indexing is as in linear arrays:

>>> space = odl.rn(3)
>>> x = space.element([1, 2, 3])
>>> x[0]
1.0
>>> x[1:]
rn(2).element([ 2.,  3.])

In higher dimensions, the i-th index expression accesses the i-th axis:

>>> space = odl.rn((2, 3))
>>> x = space.element([[1, 2, 3],
...                    [4, 5, 6]])
>>> x[0, 1]
2.0
>>> x[:, 1:]
rn((2, 2)).element(
    [[ 2.,  3.],
     [ 5.,  6.]]
)

Slices can be assigned to, except if lists are used for indexing:

>>> y = x[:, ::2]  # view into x
>>> y[:] = -9
>>> x
rn((2, 3)).element(
    [[-9.,  2., -9.],
     [-9.,  5., -9.]]
)
>>> y = x[[0, 1], [1, 2]]  # not a view, won't modify x
>>> y
rn(2).element([ 2., -9.])
>>> y[:] = 0
>>> x
rn((2, 3)).element(
    [[-9.,  2., -9.],
     [-9.,  5., -9.]]
)