Filling a preallocated array without allocations

Sure–one way to do it is just to write out the loop:

julia> function loop(kfaux::KF)
           for t in 40:100
               for j in axes(kfaux.F, 2)
                 for i in axes(kfaux.F, 1)
                   kfaux.F[i, j, j] = kfaux.F[i, j, t - 1]
                 end
               end
           end
       end
loop (generic function with 1 method)

julia> @btime loop($kfaux)
  496.222 ns (0 allocations: 0 bytes)
4 Likes