I would like to compute conditional statistics over one dimension of a two-dimensional array. I have an array a
and I would like to compute a mean over the first dimension, taking into account only the cells where b < 0.5
. With numpy
this is easy with masked arrays, but I haven’t found an Julia equivalent for this. What is the Julian way of solving this?
using Statistics
nx = 16
nz = 8
a = rand(nx, nz)
b = rand(nx, nz)
# This gives a vertical profile of 8 layers. This is the right shape,
# but without the mask applied.
a_prof = mean(a, dims=1)
println(a_prof)
# This gives a scalar, but I would like a vertical profile of 8 layers,
# with only the indices where b < 0.5 in the computation.
a_prof_b = mean(a[b .< 0.5], dims=1)
println(a_prof_b)