0

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?

foobar
  • 571
  • 1
  • 5
  • 20

3 Answers3

1

simply use Conditions.equals

first import Conditions

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

then:

myArr.getWhere(someValue, Conditions.equals(1));
amin saffar
  • 1,953
  • 3
  • 22
  • 34
  • it returns `someValue` if is exists in `myArr`. But still it is not returning the `index` in `myArr` where `someValue` exists. Maybe we have to use some function other than `getWhere` – foobar Apr 27 '20 at 17:34
1

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

raver119
  • 336
  • 1
  • 5
0

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);
    }
}