As an old Matlab user I am still sometimes struggling to understand how Julia assigns data in “arrays of arrays”:
For example, I want to copy a page of data from a 3D data array. The result is a Matrix, as expected:
A = zeros(3,3,2);
b = a[:,:,1]
julia> b
3×3 Matrix{Float64}:
** 0.0 0.0 0.0**
** 0.0 0.0 0.0**
** 0.0 0.0 0.0**
Similarly, I can replace a page of A:
A = zeros(3,3,2);
b = rand(3,3)
A[:,:,end] .= b
julia> A
3×3×2 Array{Float64, 3}:
[:, :, 1] =
** 0.0 0.0 0.0**
** 0.0 0.0 0.0**
** 0.0 0.0 0.0**
[:, :, 2] =
** 0.169528 0.718634 0.5193**
** 0.452352 0.807573 0.204051**
** 0.226577 0.223511 0.633584**
However, it doesn’t seem to work for “Arrays in Arrays” as I have naively thought. There, the extracted part b is still identical(!) to A
A = fill(rand(2,2,2),2,2)
b = A[:,:][:,:,1]
julia> A == b
true
So how can I copy a subset from A?
Analogously, it doesn’t seem to be possible to replace a subset of A by other data, like:
A = fill(rand(2,2,2),2,2)
julia> A[:,:][:,:,1] .= rand(2,2)
ERROR: MethodError: Cannot convert
an object of type Float64 to an object of type Array{Float64, 3}
I know that this is a beginners question, but it really bothers me for some time now and all my web searches were unsucessful. I would appreciate any hints. Thanks!
Alex