# Broadcast two vectors to form a matrix

**URL:** https://discourse.julialang.org/t/broadcast-two-vectors-to-form-a-matrix/81955
**Category:** General Usage
**Tags:** matrices
**Created:** [May 31, 2022, 2:35am UTC](https://discourse.julialang.org/t/broadcast-two-vectors-to-form-a-matrix/81955 "2022-05-31T02:35:28Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![lazycoder](https://avatars.discourse-cdn.com/v4/letter/l/4da419/32.png) [@lazycoder](https://discourse.julialang.org/u/lazycoder)
#### Post date: [May 31, 2022, 2:35am UTC](https://discourse.julialang.org/t/broadcast-two-vectors-to-form-a-matrix/81955/1 "2022-05-31T02:35:28Z")

</div>

I made a simple function:

```julia
f(x,y)=x+y
res=[f.(x,1:9) for x in 1:9]

```

But, now it outputs a vector of vectors. I’d like instead for it to be a matrix.  
Actually, I’m still very new to julia and I’ve grown pretty confused on weather I should use for loops or not; as much as I really like the broadcasting function for readability, nested for loops are also not that hard to read. My main concern is I have many functions like the above, and my simple approach has always been to not write a function and just do:

```julia
res=reshape(1:9,:,1) .+ reshape(1:9,1,:)

```

Having a bunch of scalar functions instead is much easier to troubleshoot, and change if required. But my functions sometimes need 3d output and do things like:

```julia
res=fill(NaN,9,9,9)
for i=1:9
    res[:,:,i]= i .+ reshape(1:9,:,1) .+ reshape(1:9,1,:)
end

```

What’s the best way to handle things like this, I definitely want better readability, but I also really want better performance.

---

<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: [May 31, 2022, 2:40am UTC](https://discourse.julialang.org/t/broadcast-two-vectors-to-form-a-matrix/81955/2 "2022-05-31T02:40:45Z")

</div>

> [@lazycoder](#):
>
> `res=reshape(1:9,:,1) .+ reshape(1:9,1,:)`

first off, it’s much easier to just:

```julia
julia> (1:9) .+ (1:9)'
9×9 Matrix{Int64}:
  2 3 4 5 6 7 8 9 10
  3 4 5 6 7 8 9 10 11
  4 5 6 7 8 9 10 11 12
  5 6 7 8 9 10 11 12 13
  6 7 8 9 10 11 12 13 14
  7 8 9 10 11 12 13 14 15
  8 9 10 11 12 13 14 15 16
  9 10 11 12 13 14 15 16 17
 10 11 12 13 14 15 16 17 18

```

> [@lazycoder](#):
>
> as much as I really like the broadcasting function for readability, nested for loops are also not that hard to read.

whichever is more clear for your use case, Julia is not a language where for-loop would be slow anyway, it doesn’t matter much as long as you try to find the way to do it such that it’s clear and minimize memory stress

---

<div class="post-metadata">

### Author: ![lazycoder](https://avatars.discourse-cdn.com/v4/letter/l/4da419/32.png) [@lazycoder](https://discourse.julialang.org/u/lazycoder)
#### Post date: [May 31, 2022, 2:53am UTC](https://discourse.julialang.org/t/broadcast-two-vectors-to-form-a-matrix/81955/3 "2022-05-31T02:53:21Z")

</div>

That’s excellent; thanks for such a quick response! That solved my first problem too! Now I have:

```julia
res=f.((1:9),(1:9)')

```

And it broadcasts properly! Now I’m only confused on how to implement more dimensions like the function:

```julia
g(x,y,z)=x+y+z

```

It should become something like:

```julia
res=fill(NaN,9,9,9)
for i=1:9
    res[:,:,i]= g.((1:9),(1:9)',i)
end

```

I guess the only thing I’m worried about is having to declare before the for loop, is that a problem?

---

<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: [May 31, 2022, 3:10am UTC](https://discourse.julialang.org/t/broadcast-two-vectors-to-form-a-matrix/81955/4 "2022-05-31T03:10:39Z")

</div>

> [@lazycoder](#):
>
> `fill(NaN,9,9,9)`

no it’s fine, pre-allocating is a pretty common pattern.

```julia
zeros(9,9,9)

```

might be easier, if you really care, you can even do:

```julia
Array{Float64, 3}(undef, 9,9,9)

```

---

<div class="post-metadata">

### Author: ![lazycoder](https://avatars.discourse-cdn.com/v4/letter/l/4da419/32.png) [@lazycoder](https://discourse.julialang.org/u/lazycoder)
#### Post date: [May 31, 2022, 3:58am UTC](https://discourse.julialang.org/t/broadcast-two-vectors-to-form-a-matrix/81955/5 "2022-05-31T03:58:32Z")

</div>

I also really like this solution:

```julia
g(x,y,z)=x+y+z
res=g.(reshape(1:9,:,1),reshape(1:9,1,:),reshape(1:9,1,1,:))

```

Do you think there is a more compact way of writing this? I haven’t timed it but it seems like it’ll be faster than pre-allocating. Actually that’s another thing that confuses me, julia documentations said that for loops are faster than broadcasting but it said pre-allocating takes up memory. So which is faster, does preallocating and a for loop take up fewer resources than broadcasting?

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [May 31, 2022, 4:52am UTC](https://discourse.julialang.org/t/broadcast-two-vectors-to-form-a-matrix/81955/6 "2022-05-31T04:52:27Z")

</div>

So `reshape(1:9, :, 1)` can be replaced by just `1:9`, and `reshape(1:9, 1, :)` by `(1:9)'`. For `reshape(1:9, 1, 1, :)` it’s not as easy, so just keep that.

Loops and broadcasting aren’t necessarily different in performance, but loops sometimes lets you avoid unnecessary allocations and do other little tricks. But here, the allocations are _not_ unnecessary, and broadcasting is much more convenient, so just use that.

---

<div class="post-metadata">

### Author: ![lazycoder](https://avatars.discourse-cdn.com/v4/letter/l/4da419/32.png) [@lazycoder](https://discourse.julialang.org/u/lazycoder)
#### Post date: [May 31, 2022, 5:07am UTC](https://discourse.julialang.org/t/broadcast-two-vectors-to-form-a-matrix/81955/7 "2022-05-31T05:07:11Z")

</div>

> [@DNF](#):
>
> Loops and broadcasting aren’t necessarily different in performance, but loops sometimes lets you avoid unnecessary allocations and do other little tricks. But here, the allocations are _not_ unnecessary, and broadcasting is much more convenient, so just use that.

Could I generalize this statement some and say: “If pre-allocation is necessary; then broadcasting might be better. If pre-allocation is unnecessary then use a for loop.”?

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [May 31, 2022, 5:11am UTC](https://discourse.julialang.org/t/broadcast-two-vectors-to-form-a-matrix/81955/8 "2022-05-31T05:11:46Z")

</div>

You can probably find counter-examples to that, but it’s not too bad. Perhaps: if broadcasting doesn’t cause unnecessary _extra_ allocations and makes your code clearer, then definitely use that.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [May 31, 2022, 8:37am UTC](https://discourse.julialang.org/t/broadcast-two-vectors-to-form-a-matrix/81955/9 "2022-05-31T08:37:05Z")

</div>

> [@DNF](#):
>
> `reshape(1:9, 1, 1, :)`

Terminating semicolons add trailing length 1 dimensions to:

```julia
[1:9;;;]

```

It is close but the dimensions are in reverse order (9×1×1) instead of (1x1x9).  
Is there any sugar for this?

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [May 31, 2022, 9:48am UTC](https://discourse.julialang.org/t/broadcast-two-vectors-to-form-a-matrix/81955/10 "2022-05-31T09:48:32Z")

</div>

I don’t know. But this will materialize the range into an allocated array, unlike the `reshape`, so I would prefer `reshape` anyway.
