Get an arbitrary slice of a Nd4j array

2019-07-05 02:42发布

问题:

I want to perform slicing in Nd4j of arbitrary sizes in the same manner as I am able to do using Numpy.

a = numpy.arange(100)
a[25:50]

The nd4j slice method only takes dimension and index arguments, not length. How can I achieve this?

回答1:

I know it's an old question but I ran across it while googling this problem exactly.

By inspecting the source code for slice I believe it can only return full rows/columns not partial from index to index. You can use method get with an NDArrayIndex instance s argument for that. This code for example is a translation of your numpy code.

import org.nd4j.linalg.api.ndarray.INDArray;
import static org.nd4j.linalg.factory.Nd4j.linspace;
import static org.nd4j.linalg.indexing.NDArrayIndex.interval;

class SliceExample {
    public static void main(String[] args) {
        INDArray a = linspace(0, 99, 100);    // up to 99 inclusive
        INDArray s = a.get(interval(25, 50)); // up to 50th non inclusive
    }
}

NDArrayIndex documentation



标签: java slice nd4j