# How to build a matrix where each element is a vector

**URL:** <https://discourse.julialang.org/t/how-to-build-a-matrix-where-each-element-is-a-vector/107674>\
**Category:** General Usage\
**Tags:** array, matrix\
**Created:** [December 15, 2023, 3:23pm UTC](https://discourse.julialang.org/t/how-to-build-a-matrix-where-each-element-is-a-vector/107674 "2023-12-15T15:23:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pex/32/49986_2.png) [@PeX](https://discourse.julialang.org/u/PeX)\
**Post date:** [December 15, 2023, 3:23pm UTC](https://discourse.julialang.org/t/how-to-build-a-matrix-where-each-element-is-a-vector/107674/1 "2023-12-15T15:23:50Z")

</div>

Hi all,  
I’m looking for the best way to construct a matrix where each element is an actual vector.  
More specifically, if I have:

```julia
fs = 10 # Sampling Frequnecy [Hz]
𝑓 = 1e-3 # Forcing frequency [Hz]
t_end = 10^4
t = collect(0.0:1/fs:t_end)
ϕ = [π/6, π/9, π/7.5, π/3]
a = [0.001, 0.003, 0.004]

```

I want to create a matrix where each element is a time-series:

```julia
Matrix[i,j] = @. sin(2*π*𝑓*t - ϕ[i]) + a[j]*t

```

I know this should be simple, but I can’t find the best way to do it for some reason.  
In addition, is there a way to build this matrix dynamically with `push!` too?

Thank you!

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [December 15, 2023, 3:29pm UTC](https://discourse.julialang.org/t/how-to-build-a-matrix-where-each-element-is-a-vector/107674/2 "2023-12-15T15:29:57Z")

</div>

```julia
[@. sin(2*π*𝑓*t - ϕ[i]) + a[j]*t for i in 1:4, j in 1:3]

```

would be one possibility (using _comprehension_, see [Single- and multi-dimensional Arrays · The Julia Language](https://docs.julialang.org/en/v1/manual/arrays/#man-comprehensions) )

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 15, 2023, 3:34pm UTC](https://discourse.julialang.org/t/how-to-build-a-matrix-where-each-element-is-a-vector/107674/3 "2023-12-15T15:34:20Z")

</div>

One other way is:

```julia-repl
julia> m = Matrix{Vector{Float64}}(undef, length(ϕ), length(a));

julia> for i in 1:4, j in 1:3
           m[i,j] = @. sin(2*π*𝑓*t - ϕ[i]) + a[j]*t
       end

```

---

<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:** [December 15, 2023, 3:36pm UTC](https://discourse.julialang.org/t/how-to-build-a-matrix-where-each-element-is-a-vector/107674/4 "2023-12-15T15:36:46Z")

</div>

> [@PeX](#):
>
> `t = collect(0.0:1/fs:t_end)`

BTW, don’t use `collect` here, there’s no point.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 15, 2023, 5:02pm UTC](https://discourse.julialang.org/t/how-to-build-a-matrix-where-each-element-is-a-vector/107674/5 "2023-12-15T17:02:49Z")

</div>

Also, a minor point, but consider using `sinpi` for stuff like this. It will be a bit more accurate and imo, cleaner.
