public class NDIndex
extends java.lang.Object
NDIndex allows you to specify a subset of an NDArray that can be used for fetching or
updating.
It accepts a different index option for each dimension, given in the order of the dimensions. Each dimension has options corresponding to:
We recommend creating the NDIndex using NDIndex(String).
NDIndex(String)| Constructor and Description |
|---|
NDIndex()
Creates an empty
NDIndex to append values to. |
NDIndex(long... indices)
Creates an NDIndex with the given indices as specified values on the NDArray.
|
NDIndex(java.lang.String indices)
Creates a
NDIndex given the index values. |
| Modifier and Type | Method and Description |
|---|---|
NDIndex |
addAllDim()
Appends a new index to get all values in the dimension.
|
NDIndex |
addBooleanIndex(NDArray index)
Updates the NDIndex by appending a boolean NDArray.
|
NDIndex |
addIndices(long... indices)
Updates the NDIndex by appending indices as specified values on the NDArray.
|
NDIndex |
addIndices(java.lang.String indices)
Updates the NDIndex by appending indices to the array.
|
NDIndex |
addSliceDim(long min,
long max)
Appends a new index to slice the dimension and returns a range of values.
|
NDIndex |
addSliceDim(long min,
long max,
long step)
Appends a new index to slice the dimension and returns a range of values.
|
NDIndexElement |
get(int dimension)
Returns the index affecting the given dimension.
|
java.util.Optional<NDIndexFullSlice> |
getAsFullSlice(Shape target)
Returns this index as a full slice if it can be represented as one.
|
java.util.List<NDIndexElement> |
getIndices()
Returns the indices.
|
int |
getRank()
Returns the number of dimensions specified in the Index.
|
static NDIndex |
sliceAxis(int axis,
long min,
long max)
Creates an
NDIndex that just has one slice in the given axis. |
java.util.stream.Stream<NDIndexElement> |
stream()
Returns a stream of the NDIndexElements.
|
public NDIndex()
NDIndex to append values to.public NDIndex(java.lang.String indices)
NDIndex given the index values.
Here are some examples of the indices format.
NDArray a = manager.ones(new Shape(5, 4, 3));
// Gets a subsection of the NDArray in the first axis.
assertEquals(a.get(new NDIndex("2")).getShape(), new Shape(4, 3));
// Gets a subsection of the NDArray indexing from the end (-i == length - i).
assertEquals(a.get(new NDIndex("-1")).getShape(), new Shape(4, 3));
// Gets everything in the first axis and a subsection in the second axis.
// You can use either : or * to represent everything
assertEquals(a.get(new NDIndex(":, 2")).getShape(), new Shape(5, 3));
assertEquals(a.get(new NDIndex("*, 2")).getShape(), new Shape(5, 3));
// Gets a range of values along the second axis that is inclusive on the bottom and exclusive on the top.
assertEquals(a.get(new NDIndex(":, 1:3")).getShape(), new Shape(5, 2, 3));
// Excludes either the min or the max of the range to go all the way to the beginning or end.
assertEquals(a.get(new NDIndex(":, :3")).getShape(), new Shape(5, 3, 3));
assertEquals(a.get(new NDIndex(":, 1:")).getShape(), new Shape(5, 4, 3));
// Uses the value after the second colon in a slicing range, the step, to get every other result.
assertEquals(a.get(new NDIndex(":, 1::2")).getShape(), new Shape(5, 2, 3));
// Uses a negative step to reverse along the dimension.
assertEquals(a.get(new NDIndex("-1")).getShape(), new Shape(5, 4, 3));
indices - a comma separated list of indices corresponding to either subsections,
everything, or slices on a particular dimensionpublic NDIndex(long... indices)
indices - the indices with each index corresponding to the dimensions and negative
indices starting from the endpublic static NDIndex sliceAxis(int axis, long min, long max)
NDIndex that just has one slice in the given axis.axis - the axis to slicemin - the min of the slicemax - the max of the sliceNDIndex with the given slice.public int getRank()
public NDIndexElement get(int dimension)
dimension - the affected dimensionpublic java.util.List<NDIndexElement> getIndices()
public final NDIndex addIndices(java.lang.String indices)
indices - the indices to add similar to NDIndex(String)NDIndexNDIndex(String)public final NDIndex addIndices(long... indices)
indices - with each index corresponding to the dimensions and negative indices starting
from the endNDIndexpublic NDIndex addBooleanIndex(NDArray index)
The NDArray should have a matching shape to the dimensions being fetched and will return where the values in NDIndex do not equal zero.
index - a boolean NDArray where all nonzero elements correspond to elements to returnNDIndexpublic NDIndex addAllDim()
NDIndexpublic NDIndex addSliceDim(long min, long max)
min - the minimum of the rangemax - the maximum of the rangeNDIndexpublic NDIndex addSliceDim(long min, long max, long step)
min - the minimum of the rangemax - the maximum of the rangestep - the step of the sliceNDIndexpublic java.util.stream.Stream<NDIndexElement> stream()
public java.util.Optional<NDIndexFullSlice> getAsFullSlice(Shape target)
target - the shape to index