# Create matrix from two vectors

**URL:** https://discourse.julialang.org/t/create-matrix-from-two-vectors/80651
**Category:** General Usage
**Tags:** arrays, broadcasting
**Created:** [May 7, 2022, 5:26am UTC](https://discourse.julialang.org/t/create-matrix-from-two-vectors/80651 "2022-05-07T05:26:58Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![paradoxical-rhapsody](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paradoxical-rhapsody/32/33021_2.png) [@paradoxical-rhapsody](https://discourse.julialang.org/u/paradoxical-rhapsody)
#### Post date: [May 7, 2022, 5:26am UTC](https://discourse.julialang.org/t/create-matrix-from-two-vectors/80651/1 "2022-05-07T05:26:58Z")

</div>

After defining a bivariate function `f(x::Int, y::Int)` , how can I get a matrix `A = ( f(a[i], b[j]) )` using two vector `a` and `b` .

For example, let `a = [1, 2]` , `b = [1, 2, 3]` and `f(x, y) = x + y` , the desirble result should be

```julia
A = [2 3 4
     5 6 7]

```

Julia’s list comprehension seems to not work. Is there any simple way to realize it?

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [May 7, 2022, 5:32am UTC](https://discourse.julialang.org/t/create-matrix-from-two-vectors/80651/2 "2022-05-07T05:32:26Z")

</div>

Hi!  
I’m not sure which comprehension syntax you tried, but this one seems to work:

```julia
julia> f(x, y) = x + y
f (generic function with 1 method)

julia> a = [1, 2];

julia> b = [1, 2, 3];

julia> A = [f(a[i], b[j]) for i = 1:2, j = 1:3]
2×3 Matrix{Int64}:
 2 3 4
 3 4 5

```

Notice the subtle difference with that one:

```julia
julia> A = [f(a[i], b[j]) for i = 1:2 for j = 1:3]
6-element Vector{Int64}:
 2
 3
 4
 3
 4
 5

```

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [May 7, 2022, 5:40am UTC](https://discourse.julialang.org/t/create-matrix-from-two-vectors/80651/3 "2022-05-07T05:40:08Z")

</div>

There’s also `f.(a, b')`.

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [May 7, 2022, 1:25pm UTC](https://discourse.julialang.org/t/create-matrix-from-two-vectors/80651/4 "2022-05-07T13:25:29Z")

</div>

```julia

[m for m in Iterators.map(Base.splat(f), Iterators.product(a,b))]

Using StatsBase
pairwise(f,a,b)

```

---

<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 7, 2022, 2:21pm UTC](https://discourse.julialang.org/t/create-matrix-from-two-vectors/80651/5 "2022-05-07T14:21:14Z")

</div>

> [@rocco\_sprmnt21](#):
>
> ```julia
> [m for m in Iterators.map(Base.splat(f), Iterators.product(a,b))]
> 
> ```

That could possibly be written as broadcasting:

```julia
Base.splat(f).(Iterators.product(a,b))

```

---

<div class="post-metadata">

### Author: ![jacobusmmsmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobusmmsmit/32/217669_2.png) [@jacobusmmsmit](https://discourse.julialang.org/u/jacobusmmsmit)
#### Post date: [May 7, 2022, 6:53pm UTC](https://discourse.julialang.org/t/create-matrix-from-two-vectors/80651/6 "2022-05-07T18:53:11Z")

</div>

Not directly related, but do you know why these two give different results?

```julia
julia> @btime broadcast(Base.splat(f), Iterators.product($a, $b))
  86.526 ns (2 allocations: 272 bytes)
3×2 Matrix{Int64}:
 2 3
 3 4
 4 5

julia> @btime map(Base.splat(f), Iterators.product($a, $b))
  46.457 ns (1 allocation: 112 bytes)
3×2 Matrix{Int64}:
 2 3
 3 4
 4 5

```

---

<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 7, 2022, 7:12pm UTC](https://discourse.julialang.org/t/create-matrix-from-two-vectors/80651/7 "2022-05-07T19:12:25Z")

</div>

I don’t know the answer. But, testing these two, to broadcast a function on an array of arrays, the speed difference leans towards broadcast:

```julia
f(x,y) = x + y
z = [rand(0:9,2) for _ in 1:1_000]
Base.splat(f).(z) # 36.9 μs (1 allocation: 7.94 KiB)
map(Base.splat(f),z) # 43.1 μs (1 allocation: 7.94 KiB)

```
