# How to tranform a vector into a matrix?

**URL:** https://discourse.julialang.org/t/how-to-tranform-a-vector-into-a-matrix/82286
**Category:** Performance
**Tags:** question
**Created:** [June 5, 2022, 11:35pm UTC](https://discourse.julialang.org/t/how-to-tranform-a-vector-into-a-matrix/82286 "2022-06-05T23:35:27Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Leticia-maria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leticia-maria/32/30981_2.png) [@Leticia-maria](https://discourse.julialang.org/u/Leticia-maria)
#### Post date: [June 5, 2022, 11:35pm UTC](https://discourse.julialang.org/t/how-to-tranform-a-vector-into-a-matrix/82286/1 "2022-06-05T23:35:27Z")

</div>

How to transform a vector of type:

```julia
[[1.021434087e-5, 1.532972083e-5, -1.493500137e-5], 
[-0.19951695340554, 0.87894179053067, -0.62713882127936], 
[0.76712229809243, 0.24863902907755, 0.74526241504934], 
[0.35580334399536, -0.82601803138729, -0.62993342769733], 
[-0.92343260142312, -0.30159515034176, 0.51179839372872]]

```

to a matrix

```julia
 1.021434087e-5 1.532972083e-5 -1.493500137e-5;
-0.19951695340554 0.87894179053067 -0.62713882127936;
 0.76712229809243 0.24863902907755 0.74526241504934;
 0.35580334399536 -0.82601803138729 -0.62993342769733;
-0.92343260142312 -0.30159515034176 0.51179839372872

```

---

<div class="post-metadata">

### Author: ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)
#### Post date: [June 5, 2022, 11:38pm UTC](https://discourse.julialang.org/t/how-to-tranform-a-vector-into-a-matrix/82286/2 "2022-06-05T23:38:25Z")

</div>

Does this work for you @Leticia-maria ?

> [@How to convert Vector of Vectors to Matrix](https://discourse.julialang.org/t/how-to-convert-vector-of-vectors-to-matrix/72609/2):
>
> What have you tried so far? The answer is julia\> mapreduce(permutedims, vcat, x) 2×3 Matrix{Int64}: 1 0 1 0 0 1

---

<div class="post-metadata">

### Author: ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)
#### Post date: [June 5, 2022, 11:56pm UTC](https://discourse.julialang.org/t/how-to-tranform-a-vector-into-a-matrix/82286/3 "2022-06-05T23:56:58Z")

</div>

```julia
julia> vcat(A'...)
5×3 Matrix{Float64}:
  1.02143e-5 1.53297e-5 -1.4935e-5
 -0.199517 0.878942 -0.627139
  0.767122 0.248639 0.745262
  0.355803 -0.826018 -0.629933
 -0.923433 -0.301595 0.511798

```

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [June 6, 2022, 6:13am UTC](https://discourse.julialang.org/t/how-to-tranform-a-vector-into-a-matrix/82286/4 "2022-06-06T06:13:01Z")

</div>

The more performant and clean way is in a package:

```julia
julia> using SplitApplyCombine

# if you need an actual materialized matrix:
julia> @btime combinedims($A, 1)
  257.150 ns (2 allocations: 192 bytes)

# if a view of the original vector is fine:
julia> @btime combinedimsview($A, 1)
  3.165 ns (0 allocations: 0 bytes)

# for comparison:
julia> @btime mapreduce(permutedims, vcat, $A)
  390.875 ns (14 allocations: 1.03 KiB)
julia> @btime vcat($A'...)
  885.152 ns (24 allocations: 768 bytes)

```

---

<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: [June 6, 2022, 8:16am UTC](https://discourse.julialang.org/t/how-to-tranform-a-vector-into-a-matrix/82286/5 "2022-06-06T08:16:29Z")

</div>

Very interesting. I have always liked the SAC package for the functions it provides and for the propensity for functional programming that I like so much.  
could you explain specifically which functions / algorithms are used internally in SAC that make this transformation (conceptually relatively simple) much faster than the other proposals?

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [June 6, 2022, 1:26pm UTC](https://discourse.julialang.org/t/how-to-tranform-a-vector-into-a-matrix/82286/6 "2022-06-06T13:26:33Z")

</div>

I don’t think SAC.jl does something special here. If you write a simple for-loop and allocate the whole resulting array upfront, performance will be the same. It’s the `mapreduce(vcat)` and `vcat(A...)` that are inefficient - the latter is fundamentally type-unstable.

As for `combinedimsview`, which is two orders of magnitude faster, it just doesn’t materialize the resulting array, providing a view instead.
