# Multidimensional array partial slice based on condition

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

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

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!

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> 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
``````
2 Likes