Return n largest nonzero entries of an array
The algorithm you described has complexity O(n * m), where m is the size of the input array, since you run a full argmax at every one of the n iterations.
You can achieve the same thing by collecting the indices of the n largest values once, and then set only those indices to 1 in the result array.  For finding of the n largest values, you could use a heap, but Julia makes this easier with partialsortperm:
function nlargest(v, n; rev=true)
    result = falses(size(v))
    result[partialsortperm(v, 1:n; rev=rev)] .= true
    return result
end

This uses a BitArray for storage; if n &lt;&lt; m, even a sparse array might pay off.  It should have complexity O(m + n log n), unless I'm misjudging partialsortperm.  Note that sorting algorithms in Julia work ascending by default, so you have to use rev=true to get the equivalent of your code.