Skip to content

Julia array tips

Convert vector of vector to matrix

Use the stack function.

stack([[1,0,1],[0,0,1]], dims=1) == [ 1 0 1 ; 0 0 1 ]

Convert linear index to 2D indices

Use CartesianIndices((nrow, ncol)) 1.

x = rand((7, 10))
CI = CartesianIndices((7, 10))
for i in 1:length(x)
    r = CI[i][1]
    c = CI[i][2]
    @assert x[i] == x[r, c]
end