答案 0 :(得分:11)
区别在于slice
会丢弃所有尺寸"切片"标量(非向量),而sub
经常保留它们。例如:
julia> A = rand(3,3)
3x3 Array{Float64,2}:
0.403464 0.229403 0.924686
0.953741 0.175086 0.49139
0.0290678 0.705564 0.567355
julia> a = slice(A, 2, :) # this will be 1-dimensional
3-element SubArray{Float64,1,Array{Float64,2},(Int64,Colon),2}:
0.953741
0.175086
0.49139
julia> b = sub(A, 2, :) # this will be 2-dimensional
1x3 SubArray{Float64,2,Array{Float64,2},(UnitRange{Int64},Colon),2}:
0.953741 0.175086 0.49139
julia> size(a)
(3,)
julia> size(b)
(1,3)
有一个例外:sub
删除用标量索引的维度,如果它们是"尾随"维度,意味着没有以向量索引的后续维度:
julia> a = slice(A, :, 2)
3-element SubArray{Float64,1,Array{Float64,2},(Colon,Int64),2}:
0.229403
0.175086
0.705564
julia> b = sub(A, :, 2)
3-element SubArray{Float64,1,Array{Float64,2},(Colon,Int64),2}:
0.229403
0.175086
0.705564
julia> size(a)
(3,)
julia> size(b)
(3,)
如果您slice
有一个范围,那么您会得到sub
之类的行为:
julia> a = slice(A, 2:2, :)
1x3 SubArray{Float64,2,Array{Float64,2},(UnitRange{Int64},Colon),1}:
0.953741 0.175086 0.49139
julia> size(a)
(1,3)
重要的不是索引的长度,而是类型:任何以非标量索引的维度都将被保留。