# Make broadcasted function return Matrix instead of Array of Arrays

**URL:** https://discourse.julialang.org/t/make-broadcasted-function-return-matrix-instead-of-array-of-arrays/55729
**Category:** General Usage
**Tags:** question
**Created:** [February 21, 2021, 5:49pm UTC](https://discourse.julialang.org/t/make-broadcasted-function-return-matrix-instead-of-array-of-arrays/55729 "2021-02-21T17:49:50Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Uroc327](https://avatars.discourse-cdn.com/v4/letter/u/eb8c5e/32.png) [@Uroc327](https://discourse.julialang.org/u/Uroc327)
#### Post date: [February 21, 2021, 5:49pm UTC](https://discourse.julialang.org/t/make-broadcasted-function-return-matrix-instead-of-array-of-arrays/55729/1 "2021-02-21T17:49:50Z")

</div>

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

```julia
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?)

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [February 21, 2021, 5:51pm UTC](https://discourse.julialang.org/t/make-broadcasted-function-return-matrix-instead-of-array-of-arrays/55729/2 "2021-02-21T17:51:52Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Uroc327](https://avatars.discourse-cdn.com/v4/letter/u/eb8c5e/32.png) [@Uroc327](https://discourse.julialang.org/u/Uroc327)
#### Post date: [February 21, 2021, 5:52pm UTC](https://discourse.julialang.org/t/make-broadcasted-function-return-matrix-instead-of-array-of-arrays/55729/3 "2021-02-21T17:52:49Z")

</div>

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