Julia's language: sub vs. function slice

Can someone explain in simple words the difference between the julia v0.4 function:

sub and slice (and possibly slicedim )

Some simple example would be very helpful. Many thanks

+7
function arrays julia-lang
source share
1 answer

The difference is that slice reduces all sizes sliced β€‹β€‹β€œsliced” using scalar (not vector), and sub often saves them. For example:

 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) 

There is one exception: sub reduces the dimensions indexed with the scalar if they are β€œfinal” dimensions, which means that later sizes are not indexed with the vector:

 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,) 

If you are slice with a range, you will get behavior similar to 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) 

It does not matter the length of the index, it is a type: any dimension indexed with Nescal will be saved.

+10
source share

All Articles