Prevent Julia from automatically converting the type of a 1D matrix slice

怎甘沉沦 提交于 2019-12-03 07:52:32
jub0bs

Use a range of length 1 instead of just an index

Instead of simply specifying the index (Int64) of the desired column, specify a range (UnitRange{Int64}) of length 1: 1:1.

That will trick Julia into preserving the 2D-array type (Array{Int64,2}) instead of returning a vector (Array{Int64,1}).

Edit: the developers discussed this topic here (thanks to Colin for pointing me to it).

julia> alpha = [1 2 3; 4 5 6]
2x3 Array{Int64,2}:
 1  2  3
 4  5  6

julia> alpha[:,1]            # nope
2-element Array{Int64,1}:
 1
 4    

julia> alpha[:,1:1]          # yep
2x1 Array{Int64,2}:
 1
 4
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!