I'm migrating from python/numpy to julia. I'm really confused by Julia's multidimensional arrays and it feels like there is some additional level of complexity / hassle (in comparison to numpy).
There is distinction between 1)row-vectros 2)column-vectors, 3)multidimensional arrays and 4)nested arrays (=Arrays of arrays). That would be all fine (perhaps useful for performance optimization), assuming there is simple way how to convert between them. But I cannot figure out how to do it.
Simple example: I just try to generate 2D rectangular grid of points and plot them
ps = [ [ix*0.1 iy*0.1] for ix=1:10, iy=1:10 ]
# 10×10 Array{Array{Float64,2},2}:
# Oh, this is nested array? I wand just simple 3D array 10x10x2
scatter( ps[:,:,1], ps[:,:,2], markersize = 2, markerstrokewidth = 0, aspect_ratio=:equal )
# ERROR: BoundsError: attempt to access 10×10 Array{Array{Float64,2},2} at index [Base.Slice(Base.OneTo(10)), Base.Slice(Base.OneTo(10)), 2]
sh = size(ps)
# (10,10)
ps = reshape( ps, ( sh[1]*sh[2],2) )
# ERROR: DimensionMismatch("new dimensions (100, 2) must be consistent with array size 100")
# Oh dear :(
ps = reshape( ps, ( sh[1]*sh[2],:) )
# 100×1 Array{Array{Float64,2},2}
xs = ps[:,1]
# 100-element Array{Array{Float64,2},1}
# ??? WTF? ... this arrays looks like whole 'ps'
ys = ps[:,2]
# ERROR: BoundsError: attempt to access 100×1 Array{Array{Float64,2},2} at index [Base.Slice(Base.OneTo(100)), 2]
xs = ps[:][1]
# 1×2 Array{Float64,2}:
# 0.1 0.1
# But I want all xs (ps[:,1]), not (ps[1,:])
# Let's try some broadcasting
xs = ps.[1]
# ERROR: syntax: invalid syntax "ps.[1]"
xs = .ps[1]
# ERROR: syntax: invalid identifier name "."
# Perhaps transpose will help?
ps_ = ps' #' stackoverflow syntax highlighting for Julia is broken ?
# 1×100 LinearAlgebra.Adjoint{LinearAlgebra.Adjoint{Float64,Array{Float64,2}},Array{Array{Float64,2},2}}:
# OMG! ... That is even worse
scatter( ps[:,1], ps[:,2], markersize = 2, markerstrokewidth = 0, aspect_ratio=:equal )
# Nope
OK this somehow works. But still I need to figure out how to convert between the different shapes of arrays above
using Plots
ps = [ [ix*0.1 iy*0.1] for ix=1:10, iy=1:10 ]
ps = vcat(ps...)
xs = ps[:,1]
ys = ps[:,2]
scatter( xs, ys, markersize = 2, markerstrokewidth = 0, aspect_ratio=:equal )
EDIT:
Maybe it could be good to list some tutorials where I was searching the answer before I asked: