# -*- coding: utf-8 -*- # --- # jupyter: # jupytext: # text_representation: # extension: .jl # format_name: light # format_version: '1.5' # jupytext_version: 1.3.3 # kernelspec: # display_name: Julia 1.6.2 # language: julia # name: julia-1.6 # --- # ### Julia Tutorial: Array Slicing # Array slicing in Julia differs a bit from Matlab. # In Matlab, everything is an array; even a scalar is a 1x1 array. # In Julia, there are distinct data types for scalars, vectors, AdjointVectors and Arrays. # This notebook illustrates how those differences affect array slicing. # For rationale, see the "Dimension sum slices" section under this blog: # https://julialang.org/blog/2016/10/julia-0.5-highlights # Jeff Fessler, University of Michigan # 2017-07-24, original # 2020-08-05, Julia 1.5.0 # 2021-08-23, Julia 1.6.2 A = [10*m+n for m=1:3, n=1:4] # start with a simple "self documenting" 2D array A[1:2,:] # slicing multiple rows or columns works the same as in Matlab A[2,:] # however, slicing a single row returns a 1D vector, not a 1×4 array! A[[2],:] # this unusual version of slicing a single row returns a 1×4 array! A[:,3] # slicing a single column yields a vector, NOT a 3×1 array A[[2],:] * A[[3],:]' # 1×4 array times a 4×1 array yields a 1×1 array! A[:,[3]] # this way gives a 3×1 array, not a vector A[:,3]' * A[:,2] # dot product between Adjoint Vector and vector yields a scalar A[:,[3]]' * A[:,[2]] # 1×3 array times a 3×1 array yields a 1×1 array! A[2,:] * A[3,:]' # this would be a dot product in Matlab # but it is an outer product in Julia.