# Multidimensional array partial slice based on condition

**URL:** <https://discourse.julialang.org/t/multidimensional-array-partial-slice-based-on-condition/22320>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 25, 2019, 7:57pm UTC](https://discourse.julialang.org/t/multidimensional-array-partial-slice-based-on-condition/22320 "2019-03-25T19:57:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Post date:** [March 25, 2019, 7:57pm UTC](https://discourse.julialang.org/t/multidimensional-array-partial-slice-based-on-condition/22320/1 "2019-03-25T19:57:58Z")

</div>

I have a multidimensional array `X` that I would like to filter by some condition `Y` defining a mask `m` as follows:

```julia
X = randn(3,5,10)
Y = randn(1,5,10)
m = Y .> 0.0
X[mask]

```

Does `X[mask]` not make sense because the resulting array would have different lengths in dimension 2? Is there a better way to approach this problem? Thanks!

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [March 25, 2019, 8:24pm UTC](https://discourse.julialang.org/t/multidimensional-array-partial-slice-based-on-condition/22320/2 "2019-03-25T20:24:13Z")

</div>

Indexing by logical arrays is defined to be equivalent to the same as indexing by the result of `findall` with two extra caveats:

- The shape of the logical array must exactly match the shape of the parent array (this one doesn’t)
- We’ve not yet implemented indexing by logical indices that span more than one dimension (but not all dimensions)

Your case is running into both of those issues. Here’s the fix. You need to drop the first dimension of `Y` (or `m`) as that doesn’t match up with the size 3 first dimension of `X`. You can do that with `dropdims`. You then need to choose which of the indices in the first dimension of `X` you want to return (e.g., `1` or `:`). And then you need to `findall` since we’ve not yet added that feature to support logical indices that span multiple dimensions (but not the entire array):

```julia
julia> X[:, findall(dropdims(m, dims=1))]
3×27 Array{Float64,2}:
 -0.174443 -0.276859 0.166649 … -1.38627 -0.353746 1.43655
 -0.24166 0.727391 2.45475 -1.57521 0.172988 -0.685829
 -1.44952 0.321046 -0.835106 -0.999816 1.52101 2.10283

```
