Explicit constructor for array of arrays?

I want to create an array of arrays explicitly. Initially I thought that this would work:

julia>  x = [ [1]  [1,2]
              []   [3,4,5] ]

but this does not work (I was expecting

2×2 Array{Any,2}:
 [1]    [1, 2]
 Any[]  [3, 4, 5]

of course).

Then I tried to use comas, semicolons, in different places, but I could not find any alternative that works. Is there one?

Just to contextualize: What I am facing is a didactic problem only. I am asking students to write a function which will deal with this array of arrays, and I want to provide that array as the input of the exercise. At this point I did not find any simpler way to initialize it than the somewhat cumbersome, for this purpose,

julia> x = Array{Any}(undef,2,2);

julia> x[1,1] = [1] ; x[1,2] = [1,2]; x[2,1] = []; x[2,2] = [3,4,5];

2 Likes

How about this:

julia> x = reshape([[1], Int[], [1,2], [3,4,5]], 2,2)
2×2 Matrix{Vector{Int64}}:
 [1]  [1, 2]
 []   [3, 4, 5]
2 Likes

Better, but still hard to visualize.

The actual example is a 4x4 matrix, thus these options are not very easy to build without running into errors:

This is what I was actually aiming to do:

cellparticles = [ [6, 10]  [5]  []  []
                  []       []   []  [4]
                  [3, 9]   []   []  [1, 7, 8]
                  []       [2]  []  []        ]


1 Like

Or this:

julia> x = [ [[1]]  [[1,2]]
             [Int[]]  [[3,4,5]] ]
2×2 Matrix{Vector{Int64}}:
 [1]  [1, 2]
 []   [3, 4, 5]

But IMO hvcat is weird tricky syntax at the best of times, and cleverly defeating it may make everything less obvious. Do they have to be arrays?

julia> cellparticles = [ (6, 10)  (5,)  ()  ()
                         ()       ()    ()  (4,)
                         (3, 9)   ()    ()  (1, 7, 8)
                         ()       (2,)  ()  ()        ]
4×4 Matrix{Tuple{Vararg{Int64, N} where N}}:
 (6, 10)  (5,)  ()  ()
 ()       ()    ()  (4,)
 (3, 9)   ()    ()  (1, 7, 8)
 ()       (2,)  ()  ()

julia> (Vector{Int}∘collect).(cellparticles)
4×4 Matrix{Vector{Int64}}:
 [6, 10]  [5]  []  []
 []       []   []  [4]
 [3, 9]   []   []  [1, 7, 8]
 []       [2]  []  []

julia> Set.(cellparticles)
4×4 Matrix{Set}:
 Set([10, 6])    Set([5])        Set{Union{}}()  Set{Union{}}()
 Set{Union{}}()  Set{Union{}}()  Set{Union{}}()  Set([4])
 Set([9, 3])     Set{Union{}}()  Set{Union{}}()  Set([7, 8, 1])
 Set{Union{}}()  Set([2])        Set{Union{}}()  Set{Union{}}()
3 Likes

Unfortunately at this point, yes. This is the result of a toy example of classifying particles in cells, and each position of the matrix contains the list of particles in each cell. Not only we didn’t talk about tuples yet, also these lists can be large.

This is the best so far :slight_smile: .

1 Like

I only want to comment that I appreciate the care you’ve taken into declare this empty array as Int[] instead of simply []. Changing this solved a type instability that made the code about 5 times slower and led to a lot of memory allocations :slight_smile: .

3 Likes