I am writing a simulation that outputs a multidimensional array A that changes over time, and whose ndims depends on the input dimension (1 or 2 for now). I would like to save A over time in a larger array A_t in my for-loop, for which I used CartesianIndices. The following is a MWE:
A_t = zeros(10,2) A = ones(10) @btime A_t[CartesianIndices(A),1] = A @btime A_t[CartesianIndices(A),1] .= A
133.829 ns (1 allocation: 16 bytes) 1.287 μs (8 allocations: 176 bytes)
Clearly, not broadcasting is a lot faster.
A_t = zeros(10,10,2) A = ones(10,10) # @btime A_t[CartesianIndices(A),1] = A @btime A_t[CartesianIndices(A),1] .= A
Now, not broadcasting throws the following error:
DimensionMismatch("tried to assign 10×10 array to 100×1 destination")
It appears that appending a 1 at the end LinearIndexes the CartesianIndices() object. I would like to append a 1 behind all the CartesianIndices without broadcasting (which would be slower in 1D), but have not found a way to do so.
Any advice? Thanks!