Assign multidimensional array blocks

Imagine you have a 3-dimensional Julia A array of type Float64, where size(A) = (2, 3, 3) .

How could you assign blocks to this array at a time using two-dimensional arrays? For example, let's say I wanted A[1, :, :] 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 that this is because size(A[1, :, :]) = (1, 3, 3) , but I can’t figure out how to either 1) get this slice as easy as (3, 3) , therefore eye(3) fits or 2) make eye(3) also be (1, 3, 3) to fit the shape of slice A

Any suggestions?

EDIT 12:51 AM PST 8-13-13


I learned two new things:

  • If I take a slice A along either of the other two dimensions, the result will be a 2-dimensional array instead of a 3-d array with a leading size of 1.
  • I found a workaround for my specific problem by doing A[1, :, :] = reshape(eye(3), (1, 3, 3)) . This is not optimal, and I hope for a better fix.
+4
source share
1 answer

You might 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 
+7
source

Source: https://habr.com/ru/post/1496725/


All Articles