foobar
foobar

Reputation: 619

Nd4J: get index where a value exists

how to get index where a certain value exists. In numpy:

import numpy as np
myArr = np.array()
index = np.where(myArr == someValue)
// Output: an index value consisting rows and cols will be given

In ND4J, I have reached here, but I dont know what to put in condition parameter:

INDArray index = myArr.getWhere(someValue, condition=??);

In other words, how to find an element in INDArray in ND4J?

Upvotes: 0

Views: 437

Answers (3)

Filip Pavičić
Filip Pavičić

Reputation: 71

You can use org.nd4j.linalg.factory.Nd4j where function, just provide null as second and third argument.

import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;


public class Main1 {
    public static void main(String[] args) {
        INDArray array = Nd4j.create(new double[]{1.0, 4.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0});

        double someValue = 1.0;
        INDArray indexes = Nd4j.where(array.eq(someValue), null, null)[0];

        System.out.println(indexes);
    }
}

Upvotes: 1

raver119
raver119

Reputation: 336

BooleanIndexing.firstIndex(INDArray, Condition) is what you're looking i think.

Upvotes: 1

amin saffar
amin saffar

Reputation: 2033

simply use Conditions.equals

first import Conditions

import org.nd4j.linalg.indexing.conditions.Conditions;

then:

myArr.getWhere(someValue, Conditions.equals(1));

Upvotes: 1

Related Questions