Getting 1D Subsets of Multi dimensional arrays in julia

∥☆過路亽.° 提交于 2019-12-06 07:24:43

A quick hack to get 1D arrays is to append [:] to your array references. E.g.,

julia> A = rand(Int,3,3,3)
3x3x3 Array{Int32,3}:
[:, :, 1] =
  1059011904  -1092196516  -2083742447
 -1232110963     46419394    599245389
   747779547   1800837260   -460798437

[:, :, 2] =
  -154984919  1641929284  1335793910
 -1575337246  1100743707   333491108
   231729201  1543773782   338937245

[:, :, 3] =
 -1812252712    374672056  -156561770
   317145782  -1941995702   747015018
   127966143   -102265949  1068453724

julia> A[:,1,1]
3-element Array{Int32,1}:
  1059011904
 -1232110963
   747779547

julia> A[:,1,1][:]
3-element Array{Int32,1}:
  1059011904
 -1232110963
   747779547

julia> A[1,:,1]
1x3 Array{Int32,2}:
 1059011904  -1092196516  -2083742447

julia> A[1,:,1][:]
3-element Array{Int32,1}:
  1059011904
 -1092196516
 -2083742447

julia> A[1,1,:]
1x1x3 Array{Int32,3}:
[:, :, 1] =
 1059011904

[:, :, 2] =
 -154984919

[:, :, 3] =
 -1812252712

julia> A[1,1,:][:]
3-element Array{Int32,1}:
  1059011904
  -154984919
 -1812252712

You may also be interested in the ArrayViews package which may be more efficient depending on what you want to implement.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!