Imagine you have a 3 dimensonal Julia Array A
of type Float64 where size(A) = (2, 3, 3)
.
How could you assign blocks of this array at a time using 2-dimensional arrays? For example, say I wanted A[1, :, :]
to be the identity matrix. I would think of doing something like this:
A = Array(Float64, 2, 3, 3)
A[1, :, :] = eye(3)
When I do this I get the following error:
ERROR: argument dimensions must match
in setindex! at array.jl:592
I know it is because size(A[1, :, :]) = (1, 3, 3)
, but I can't figure out how to either 1) get this slice to be just (3, 3)
so eye(3)
fits or 2) make eye(3)
also be (1, 3, 3)
to conform to the shape of the slice of A
.
Any suggestions?
I learned two new things:
A
along either of the other two dimensions, the result is a 2-dimensional array instead of a 3-d array with the leading dimension being 1.A[1, :, :] = reshape(eye(3), (1, 3, 3))
. This is sub-optimal and I am hoping for a better fix.You may be looking for slice
:
julia> sA = slice(A, 1, :, :)
3x3 SubArray of 2x3x3 Float64 Array:
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
julia> sA[:] = eye(3)
3x3 Float64 Array:
1.0 0.0 0.0
0.0 1.0 0.0
0.0 0.0 1.0
julia> A
2x3x3 Float64 Array:
[:, :, 1] =
1.0 0.0 0.0
0.0 0.0 0.0
[:, :, 2] =
0.0 1.0 0.0
0.0 0.0 0.0
[:, :, 3] =
0.0 0.0 1.0
0.0 0.0 0.0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With