Maybe this one is more elegant (though probably slower)?
m = rand(3,3)
indices = CartesianIndices(m)
result = [I in diag(indices) ? f(m[I]) : g(m[I]) for I in indices]
Here’s another option using broadcasting:
m = rand(3,3)
I = diagind(m)
J = setdiff(LinearIndices(m), I)
result = similar(m)
result[I] .= f.(m[I])
result[J] .= g.(m[J])
and with the InvertedIndices.jl package you can replace setdiff(LinearIndices(m), I) with Not(I).
(Edit: @Seif_Shebl made me realize I had inadvertently removed the broadcasting assignment.)