Indexing and Slicing
Accessing and modifying elements, rows, columns, and sub-arrays of INDArrays using NDArrayIndex
A note on views vs. copies
INDArray rowCopy = myArray.getRow(0).dup(); // independent copy1. Getting and Setting Individual Values
Reading single values
INDArray arr = Nd4j.create(new double[][]{
{1.0, 2.0, 3.0},
{4.0, 5.0, 6.0},
{7.0, 8.0, 9.0}
});
// getDouble(int row, int col) -- 2D shorthand
double val = arr.getDouble(1, 2);
// 6.0
// getDouble(int...) -- works for any rank
double same = arr.getDouble(1, 2);
// 6.0
// getFloat / getInt -- same signatures, different return type
float f = arr.getFloat(0, 0);
// 1.0
int i = arr.getInt(2, 1);
// 8Writing single values: putScalar
putScalar2. Row and Column Access
Getting rows
Getting multiple rows
Setting a row: putRow
putRowGetting a column
3. NDArrayIndex-Based Access
NDArrayIndex.point(int)
NDArrayIndex.point(int)NDArrayIndex.all()
NDArrayIndex.all()NDArrayIndex.interval(int from, int to)
NDArrayIndex.interval(int from, int to)NDArrayIndex.interval(int from, int stride, int to)
NDArrayIndex.interval(int from, int stride, int to)NDArrayIndex.specified(long...)
NDArrayIndex.specified(long...)Combining NDArrayIndex types
Extending to 1D arrays
4. Put Operations: Writing to Sub-Arrays
put(INDArrayIndex[], INDArray)
put(INDArrayIndex[], INDArray)Equivalence with get().assign()
get().assign()Assigning a scalar to a slice
5. Tensor Along Dimension
Core method signatures
2D example
Shape rules for TAD
Input shape
TAD dimensions
Number of tensors
Tensor shape
3D example: iterating over 2D slices
6. Converting to Java Arrays
1D arrays
2D arrays
7. NdIndexIterator: Sequential Traversal
8. Boolean Indexing
Replace values that match a condition
Apply a value from a second array where condition holds
Available conditions
Factory method
Meaning
9. Views in Depth: What Is and Is Not a View
Operations that return views
Operations that return copies
Forcing a copy
Quick Reference
Goal
Method
Returns
See Also
Last updated
Was this helpful?