Trouble Understanding Slicing

The reason scalar indexing drops dimensions is a little more subtle than I explained initially. The idea is that A[i,:][j]=A[i,j] is an identity that feels like it should hold. Since A[i,j] is a scalar, A[i,:] needs to be a type which when indexed by 1 index gives a scalar. The fact that Julia has ND arrays makes this harder to get around since matrices aren’t consider special-- they are just Array{T,2}.

The reason A[[i],:] needs to have the same type as A[[i,j],:] is for type stability. Both of these are calls to getindex(::Vector{Int}, ::COLON_TYPE), so if you want type stable code (which is critical for performance) both of these need to have the same type.

1 Like