FYI, it would be much better to make your B 3x3x2.
Your A is a 3x3 mask (based on > 0.5 or not), your B has 2 “layers” of 3x3 matrix.
My understanding is that you want to assign random values to each of those two layers at location according to mask A.
julia> for i = CartesianIndices(A)
if A[i] > 0.5
@. B[:, i] = rand()
end
end
this should do?