Make broadcasted function return Matrix instead of Array of Arrays

I have a function returning a (fixed and known-length) array for some input:

function bitlevels(n, x)
    l = reverse(digits(x, base=2))
    [repeat([0], n - length(l)); l]
end

When broadcasting this function to a vector (e.g., bitlevels.(5, [0, 2, 3, 5])), I get an Array of Arrays instead of a Matrix.

I’d assume this has performance penalties due to pointer indirection, when those arrays become large. How can I change my implementation, so that a Matrix instead of an Array of Arrays is returned?

(Or is this actually a bad idea and I’m better off working on Arrays of Arrays?)

if your function returns a vector, then broadcasting (by construction) can only return a vector of vector. you either need to cat the resul manually, or you need to map reduce and cat along the way

Okay, thanks. So I’ll check the performance, how copying to a matrix compares to working on arrays of arrays.