# Repeat and reshape vector into a matrix

**URL:** <https://discourse.julialang.org/t/repeat-and-reshape-vector-into-a-matrix/127451>\
**Category:** General Usage\
**Tags:** vector, reshaping, matrix\
**Created:** [March 28, 2025, 11:41am UTC](https://discourse.julialang.org/t/repeat-and-reshape-vector-into-a-matrix/127451 "2025-03-28T11:41:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ju\_ska](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@Ju\_ska](https://discourse.julialang.org/u/Ju_ska)\
**Post date:** [March 28, 2025, 11:41am UTC](https://discourse.julialang.org/t/repeat-and-reshape-vector-into-a-matrix/127451/1 "2025-03-28T11:41:12Z")

</div>

Hi,

this is a Python code I’m translating in Julia:

```julia
import numpy as np

val = [1,2,3,4,5,6]
val_length = len(val)
rpt = 3

np.repeat(val, rpt).reshape((val_length, rpt))

```

It gives:

```julia
>>> val = [1,2,3,4,5,6]
>>> val_length = len(val)
>>> 
>>> rpt = 3
>>> np.repeat(val, rpt).reshape((val_length, rpt))
array([[1, 1, 1],
       [2, 2, 2],
       [3, 3, 3],
       [4, 4, 4],
       [5, 5, 5],
       [6, 6, 6]])

```

I used Julia’s repeat for the first part, but the reshape part is driving me crazy. I tried many combinations of reshape, vcat, reduce(vcat… and so on and I never ever had the wanted result.  
I’m quite ashamed and I can’t end up pushing into empty arrays as usual when I’m stuck because I have a great need for performance as this block of code will run gazillions of times.

Any help is welcome.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [March 28, 2025, 11:56am UTC](https://discourse.julialang.org/t/repeat-and-reshape-vector-into-a-matrix/127451/2 "2025-03-28T11:56:41Z")

</div>

You mean like this?

```julia
julia> [0+i for i ∈ 1:6, j ∈ 1:3]
6×3 Matrix{Int64}:
 1 1 1
 2 2 2
 3 3 3
 4 4 4
 5 5 5
 6 6 6

```

although when you say

> I have a great need for performance as this block of code will run gazillions of times.

this sounds incompatible with materialising a matrix with lots of (potentially?) redundant entries.

---

<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:** [March 28, 2025, 12:04pm UTC](https://discourse.julialang.org/t/repeat-and-reshape-vector-into-a-matrix/127451/3 "2025-03-28T12:04:37Z")

</div>

Since you’re not in Python anymore [sigh of relief] you can also just preallocate a matrix with the correct size and fill its coefficients within a for loop. Usually it is more explicit and not necessarily slower than the vectorized approach.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [March 28, 2025, 12:18pm UTC](https://discourse.julialang.org/t/repeat-and-reshape-vector-into-a-matrix/127451/4 "2025-03-28T12:18:43Z")

</div>

The translation you were trying to do with standard functions would be:

```julia
julia> reshape(repeat(1:6, 3), 6, 3)
6×3 Matrix{Int64}:
 1 1 1
 2 2 2
 3 3 3
 4 4 4
 5 5 5
 6 6 6

```

It’s not memory efficient, not in NumPy either. If you need a `Matrix` though, the most you can improve things is save an allocation or two.

---

<div class="post-metadata">

**Author:** ![karei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karei/32/214809_2.png) [@karei](https://discourse.julialang.org/u/karei)\
**Post date:** [March 28, 2025, 12:25pm UTC](https://discourse.julialang.org/t/repeat-and-reshape-vector-into-a-matrix/127451/5 "2025-03-28T12:25:46Z")

</div>

If the matrix you want is:

```julia
6×3 Matrix{Int64}:
 1 1 1
 2 2 2
 3 3 3
 4 4 4
 5 5 5
 6 6 6

```

then here are three ways:

```julia
val = [1:6;]
@benchmark repeat(val, 1, 3)
# 128.698 ns Memory estimate: 304 bytes, allocs estimate: 3.
@benchmark val .* ones(Int, 1, 3)
# 117.355 ns Memory estimate: 320 bytes, allocs estimate: 3.
buffer = Matrix{Int}(undef, 6, 3)
@benchmark buffer .= val 
# 53.311 ns Memory estimate: 16 bytes, allocs estimate: 1.

```

---

<div class="post-metadata">

**Author:** ![TZ.Neumann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tz.neumann/32/34438_2.png) [@TZ.Neumann](https://discourse.julialang.org/u/TZ.Neumann)\
**Post date:** [March 28, 2025, 1:17pm UTC](https://discourse.julialang.org/t/repeat-and-reshape-vector-into-a-matrix/127451/6 "2025-03-28T13:17:05Z")

</div>

Use in-place broadcasting and @inbounds macro which is safe here coz output is pre-allocated to the correct size (length(val), rpt).

```Julia
repeat_reshape!(output, val, rpt) = (@inbounds output .= reshape(val, :, 1); output)

val = [1:6;]
rpt = 3
output = Matrix{Int}(undef, length(val), rpt) # pre-allocate output matrix
result = repeat_reshape!(output, val, rpt)

```

---

<div class="post-metadata">

**Author:** ![Ju\_ska](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@Ju\_ska](https://discourse.julialang.org/u/Ju_ska)\
**Post date:** [March 28, 2025, 1:32pm UTC](https://discourse.julialang.org/t/repeat-and-reshape-vector-into-a-matrix/127451/7 "2025-03-28T13:32:25Z")

</div>

Thanks everyone.  
Your answers made me realize that the Python code was too complex for this simple task, and as I was looking for a 1:1 algorithm I lost myself.

```julia
julia> val = [1,2,3,4,5,6]
6-element Vector{Int64}:
 1
 2
 3
 4
 5
 6

julia> rpt = 3
3

julia> repeat(val, 1, rpt)
6×3 Matrix{Int64}:
 1 1 1
 2 2 2
 3 3 3
 4 4 4
 5 5 5
 6 6 6

```

---

<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:** [March 28, 2025, 1:46pm UTC](https://discourse.julialang.org/t/repeat-and-reshape-vector-into-a-matrix/127451/8 "2025-03-28T13:46:34Z")

</div>

> [@nilshg](#):
>
> although when you say
> 
> > I have a great need for performance as this block of code will run gazillions of times.
> 
> this sounds incompatible with materialising a matrix with lots of (potentially?) redundant entries.

💯 If you care about the performance of this operation, you’re probably doing something wrong. How are you planning to _use_ this matrix?

---

<div class="post-metadata">

**Author:** ![Ju\_ska](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@Ju\_ska](https://discourse.julialang.org/u/Ju_ska)\
**Post date:** [March 28, 2025, 3:34pm UTC](https://discourse.julialang.org/t/repeat-and-reshape-vector-into-a-matrix/127451/9 "2025-03-28T15:34:02Z")

</div>

You’re totally right, this part of the Python code (not written by me) is quite ugly.  
But for now I’m in the 1:1 part of the translation as I want to run an operational code ASAP. I’ll optimize later.

---

<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:** [March 28, 2025, 9:13pm UTC](https://discourse.julialang.org/t/repeat-and-reshape-vector-into-a-matrix/127451/10 "2025-03-28T21:13:31Z")

</div>

If you’re optimizing later, why are you worried about the performance of this piece of the computation? You’re almost certainly going to eliminate it completely later.

(There’s a good chance that line-by-line translation of Pythonic code will get _slower_ in Julia. Don’t be discouraged by this — idiomatic Julia code has lots of ways to speed things up that are unavailable in Python, because loops in Julia are fast. A line-by-line translation may be useful for correctness checking, but eventually the code is likely to look completely different since you don’t have to bend over backwards to “vectorize” everything.)
