How to stores arrays in matrices

This is a bit of an unusual matrix, so I cannot find a nice way of constructing it with literals (not sure if it’s possible or not), but I would do this, which is pretty readable:

str = [["a", "b", "c"],
       ["d", "e", "f"],
       ["g", "h"],
       ["i", "j", "k", "lm"]]

num = [1, 2, 4, 9]

a = [str num]

As I said, this is a slightly odd matrix, are you sure that a matrix is the right data structure for what you are doing? Julia has a lot of nice data structures. If you are used to only using arrays, then it’s easy to overlook them.

Edit: Here’s a one-liner:

a = [[["a", "b", "c"], ["d", "e", "f"], ["g", "h"], ["i", "j", "k", "lm"]] [1, 2, 4, 9]]

Less readable, imho.

2 Likes