# Help with and possible bug in BooleanIndexing

**URL:** <https://community.konduit.ai/t/help-with-and-possible-bug-in-booleanindexing/1501>\
**Category:** ND4J\
**Created:** [July 15, 2021, 7:53pm UTC](https://community.konduit.ai/t/help-with-and-possible-bug-in-booleanindexing/1501 "2021-07-15T19:53:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![wcneill](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/wcneill/32/559_2.png) [@wcneill](https://community.konduit.ai/u/wcneill)\
**Post date:** [July 15, 2021, 7:53pm UTC](https://community.konduit.ai/t/help-with-and-possible-bug-in-booleanindexing/1501/1 "2021-07-15T19:53:00Z")

</div>

I’m looking to replicate some behavior in NumPy, namely slicing/indexing with booleans:

```auto
import numpy as np
a = np.arange(10)
mask = a < 5
a[mask]

# array([0, 1, 2, 3, 4])

```

I have found and played with the `BooleanIndexing` package and am close to a solution. The application is taking an integer list of cluster assignments and building the clusters from them. Here’s a stripped down example:

```auto
        int[] assignments = {0,0,0,1,0,2,2};
        int[] indexes = {0,1,2,3,4,5,7};
        List<int[]> clusters = new ArrayList<>();

        INDArray asarray = Nd4j.createFromArray(assignments);
        INDArray idxarray = Nd4j.createFromArray(indexes);

        int i = 1; 
        INDArray mask = asarray.match(i, Conditions.equals());
        INDArray cluster = BooleanIndexing.applyMask(idxarray, mask);
        clusters.add(cluster.toIntVector());

```

However, this results in the following clusters like the following:

```auto
[0, 1, 2, 0, 4, 0, 0, 0, 0, 0, ...

```

There are a couple of issues here, one of which might be a bug:

1. The first data point is assigned to all clusters because its index is zero.
2. Lots of extraneous zeros.
3. The mask is incorrect after i=0 (maybe bug?):

For example, the code (the mask) above yields the result ` [false, false, false, true, false, true, true]`. Notice that the last two booleans are true, which implies that (2 == 1) is true.

So I’m at a bit of a loss as to how to continue, and whether or not this is a bug I should report. Is there a method like `.getWhere()` that takes a boolean array and returns only those elements that correspond to `true` values?

---

<div class="post-metadata">

**Author:** ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)\
**Post date:** [July 16, 2021, 11:27pm UTC](https://community.konduit.ai/t/help-with-and-possible-bug-in-booleanindexing/1501/2 "2021-07-16T23:27:43Z")

</div>

@wcneill go ahead and report an issue and then make sure there’s a clear test case (eg: this is fine) and we’ll get this fixed next week and you can then try it in snapshots.
