# How do I make a matrix of vectors?

**URL:** <https://discourse.julialang.org/t/how-do-i-make-a-matrix-of-vectors/132765>\
**Category:** General Usage\
**Tags:** matrix\
**Created:** [September 30, 2025, 6:06am UTC](https://discourse.julialang.org/t/how-do-i-make-a-matrix-of-vectors/132765 "2025-09-30T06:06:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![phma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phma/32/6576_2.png) [@phma](https://discourse.julialang.org/u/phma)\
**Post date:** [September 30, 2025, 6:06am UTC](https://discourse.julialang.org/t/how-do-i-make-a-matrix-of-vectors/132765/1 "2025-09-30T06:06:23Z")

</div>

I’m cryptanalyzing a hash function I invented. I get a big amount of data which I want to return as a matrix

```julia-auto
normalize(cd0a) normalize(cd0b)
normalize(cd1a) normalize(cd1b)

```

where the two rows represent using the three S-boxes in a different order and the two columns represent different plaintexts. Each `normalize(cd)` is a 40-element vector (the number varies) of 255-element vectors of 256-element offset vectors (the number varies, if the whole thing has 44 elements then each innermost offset vector has 288 elements, etc.).

Here’s the end of my function:

```julia-auto
  end # of a for loop that computes all the data
  ([round1same0,round2same0,round1same1,round2same1]./(2*iters),
   [normalize(cd0a) normalize(cd0b);normalize(cd1a) normalize(cd1b)])
  #normalize(cd0a)
end

```

The commented-out line is for debugging; if I uncomment it, I get a 40-element vector. With it commented out, I get a tuple where the first element is a 4-element vector (and it’s correct) and the second element is not the expected 2×2 matrix of 40-element vectors but an 80×2 matrix of 255-element vectors (two 40-element vectors have been concatenated). How do I prevent concatenation and get a 2×2 matrix of vectors?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 30, 2025, 6:36am UTC](https://discourse.julialang.org/t/how-do-i-make-a-matrix-of-vectors/132765/2 "2025-09-30T06:36:36Z")

</div>

> [@phma](#):
>
> the second element is not the expected 2×2 matrix of 40-element vectors but an 80×2 matrix of 255-element vectors (two 40-element vectors have been concatenated). How do I prevent concatenation and get a 2×2 matrix of vectors?

You can do

```julia-auto
reshape([a,b,c,d], 2,2)

```

to get a 2×2 matrix of vectors, if `a,b,c,d` are vectors.

---

<div class="post-metadata">

**Author:** ![phma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phma/32/6576_2.png) [@phma](https://discourse.julialang.org/u/phma)\
**Post date:** [September 30, 2025, 6:44am UTC](https://discourse.julialang.org/t/how-do-i-make-a-matrix-of-vectors/132765/3 "2025-09-30T06:44:41Z")

</div>

Thanks! (Code is still running but I tried `reshape([1,2,3,4],2,2)`.) It should be `reshape([normalize(cd0a),normalize(cd1a),normalize(cd0b),normalize(cd1b)],2,2)` to get what I want.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [September 30, 2025, 9:11am UTC](https://discourse.julialang.org/t/how-do-i-make-a-matrix-of-vectors/132765/4 "2025-09-30T09:11:45Z")

</div>

> [@phma](#):
>
> How do I prevent concatenation and get a 2×2 matrix of vectors?

It’s not really better than the reshape but another option is to fool the concatenation to work on a wrapping vector.

```julia-auto
julia> a, b, c, d = rand(2), rand(2), rand(2), rand(2);

julia> [[a] [b]
        [c] [d]]
2×2 Matrix{Vector{Float64}}:
 [0.668767, 0.556128] [0.776861, 0.865317]
 [0.170214, 0.638821] [0.0869913, 0.647021]

```

---

<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:** [September 30, 2025, 12:23pm UTC](https://discourse.julialang.org/t/how-do-i-make-a-matrix-of-vectors/132765/5 "2025-09-30T12:23:24Z")

</div>

An alternative approach is to use TensorCast.jl, which provides control of where each vector element goes in the built matrix:

```julia
using TensorCast

V = [a, b, c, d]
@cast M[i,j] := V[i⊗j] i in 1:2 # "⊗" can be typed by \otimes<tab>

```

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [September 30, 2025, 4:44pm UTC](https://discourse.julialang.org/t/how-do-i-make-a-matrix-of-vectors/132765/6 "2025-09-30T16:44:50Z")

</div>

In my eyes the most straightforward approach is to use `Matrix{Vector{Vector{Vector{Float64}}}}(undef, 2, 2)` or whatever is the appropriate type in your case, and fill it in your loop.

---

<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:** [September 30, 2025, 5:44pm UTC](https://discourse.julialang.org/t/how-do-i-make-a-matrix-of-vectors/132765/7 "2025-09-30T17:44:08Z")

</div>

Maybe you meant to use an expression like that 🙂

```julia-auto
a,b,c,d=[rand(40) for _ in 1:4]

[[a] [b]; [c] [d]] 

```
