I want to efficiently build up a large matrix in a loop. At every iteration I will:
- Perform some linear algebra operations on the matrix (it’s more complicated than the example below), and
- Append a new row to the matrix.
The problem I have is that whenever I reshape my array buffer into a matrix, it gets marked as “shared”, even though the matrix representation is temporary and goes out of scope.
Is there a way to “unshare” the array? Or is there a better approach to this problem?
julia> x = rand(2);
julia> for _ in 1:10000
println(reshape(x, 2, :) |> y -> y * y')
append!(x, rand(2))
end
[0.009747815392725486 0.039514699187714185; 0.039514699187714185 0.16018065474042284]
ERROR: cannot resize array with shared data
Stacktrace:
[1] _growend!
@ ./array.jl:922 [inlined]
[2] append!(a::Vector{Float64}, items::Vector{Float64})
@ Base ./array.jl:1019
[3] top-level scope
@ REPL[658]:3