Overwrite part of baseline array in a for loop meeting certain conditions

Question perhaps best posed in a MWE:

function foo()
    A = zeros(3)
    A[2] = 1
    for i = 2:3
        ind = findall(A .== 1)
        A[ind] .= i
        @show(A)
    end
end

julia> foo()
A = [0.0, 2.0, 0.0]
A = [0.0, 2.0, 0.0]

My desired result is:

julia> foo()
A = [0.0, 2.0, 0.0]
A = [0.0, 3.0, 0.0]

I’d like to overwrite specific indices of the baseline array A. The values change with each loop iteration, but the indices don’t.

I could, inside the for loop, create a copy B = copy(A), and then find the indices matching my criteria. This is my current solution, but I suspect there’s a better one.

I could retain the indices, but I think I’d still have to create a copy of the baseline A inside the for loop, and this would make for longer code.

What is the best way to go about this? I assumed the array A inside the for loop was different than that outside of it, with everything being wrapped in a function.

Appreciate any insight!

It seems like just moving the ind = findall(A .== 1) outside of the loop would solve the problem, wouldn’t it?

julia> function foo()
           A = zeros(3)
           A[2] = 1
           ind = findall(A .== 1)
           for i in 2:3
               A[ind] .= i
               @show(A)
           end
       end
foo (generic function with 1 method)

julia> foo()
A = [0.0, 2.0, 0.0]
A = [0.0, 3.0, 0.0]

Yup, that was my 2nd proposed solution where I “retain the indices”. I figured I would have to copy the array inside the loop, but I see in your MWE that’s not necessary.

My only (minor) issue is that, in reality, there are 4 sets of indices, so I would have to save ind1, ind2, ind3, and ind4 outside of the for loop. Not a big deal, but I was wondering if there was a cleaner way.

I think I oversimplified my MWE, so let me expand just a bit.

I have a baseline array A that has frequency-dependent properties. Certain parts of the array will take on different values depending on the current frequency iteration (or i in my MWE).

The baseline array A is given identifiers (e.g., A=1, A=2, A=3, etc.) at locations where the values will be overwritten. So it’s these identifiers that I’d like to be able to access as I sweep through i.

function apply_materials(A,n)
    ind     = findall(A .== 1)
    A[ind] .= n
end

function foo()
    A = zeros(3)
    A[2] = 1
    for i = 1:3
        A = apply_materials(A,n)
    end
end

I definitely think I oversimplified my MWE, so I hope my notes are somewhat clear.