Edit:
The fast way to do it is:
@views @. img[:, :, 2] *= img[:, :, 2] != img[:, :, 3]
and here is a benchmark:
julia> img = rand(1:3, 10000, 10000, 3);
julia> @time @views @. img[:, :, 2] *= img[:, :, 2] != img[:, :, 3];
0.111409 seconds (28 allocations: 1.219 KiB)
There are alternative ways to do what you want, but if we want to follow your implementation then do:
julia> img = rand(1:3, 4,4,3)
4×4×3 Array{Int64,3}:
[:, :, 1] =
1 3 2 3
1 1 1 3
3 2 1 3
2 2 3 1
[:, :, 2] =
1 2 3 2
2 2 1 1
3 3 3 2
1 3 2 1
[:, :, 3] =
3 2 2 2
3 2 2 1
3 2 3 3
2 2 1 2
julia> mask = img[:, :, 2] .== img[:, :, 3]
4×4 BitArray{2}:
0 1 0 1
0 1 0 1
1 0 1 0
0 0 0 0
julia> view(img,:, :, 2)[mask] .= 0;
julia> img
4×4×3 Array{Int64,3}:
[:, :, 1] =
1 3 2 3
1 1 1 3
3 2 1 3
2 2 3 1
[:, :, 2] =
1 0 3 0
2 0 1 0
0 3 0 2
1 3 2 1
[:, :, 3] =
3 2 2 2
3 2 2 1
3 2 3 3
2 2 1 2
(it is crucial to use view to get what you want, as otherwise img[:, :, 2] creates a copy)
Alternatively in Julia it is easy enough to write the same using the loop e.g.:
function applymask!(img)
for i in axes(img, 1), j in axes(img, 2)
img[i, j, 2] == img[i, j, 3] && (img[i, j, 2] = 0)
end
end
(this is not the fastest possible implementation as it uses branching and does bounds checking, but it should be good enough in most cases)
And now you can write:
julia> img = rand(1:3, 4,4,3)
4×4×3 Array{Int64,3}:
[:, :, 1] =
1 3 1 3
3 3 1 2
3 3 3 3
2 1 3 1
[:, :, 2] =
3 1 3 1
3 3 3 3
2 1 1 3
1 2 3 3
[:, :, 3] =
1 1 2 1
2 1 3 2
3 3 1 1
1 2 1 3
julia> applymask!(img)
julia> img
4×4×3 Array{Int64,3}:
[:, :, 1] =
1 3 1 3
3 3 1 2
3 3 3 3
2 1 3 1
[:, :, 2] =
3 0 3 0
3 3 0 3
2 1 0 3
0 0 3 0
[:, :, 3] =
1 1 2 1
2 1 3 2
3 3 1 1
1 2 1 3