Assign blocks of multi-dimensional array

julia

Solution

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

Problem

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? EDIT 12:51 AM PST 8-13-13 I learned two new things: - If I take a slice of `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. - I found a temporary fix to my specific problem by doing `A[1, :, :] = reshape(eye(3), (1, 3, 3))`. This is sub-optimal and I am hoping for a better fix.

Original source