# Get the transpose of Vector{Vector{Float64}}

**URL:** https://discourse.julialang.org/t/get-the-transpose-of-vector-vector-float64/128141
**Category:** New to Julia
**Tags:** arrays, vector, comprehension
**Created:** [April 16, 2025, 7:38pm UTC](https://discourse.julialang.org/t/get-the-transpose-of-vector-vector-float64/128141 "2025-04-16T19:38:47Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![15et7dtwq](https://avatars.discourse-cdn.com/v4/letter/1/a8b319/32.png) [@15et7dtwq](https://discourse.julialang.org/u/15et7dtwq)
#### Post date: [April 16, 2025, 7:38pm UTC](https://discourse.julialang.org/t/get-the-transpose-of-vector-vector-float64/128141/1 "2025-04-16T19:38:47Z")

</div>

I am coming from numpy, trying tosee if Julia can be faster. Right now I am dealing with a function that generates for me what I thought had to be a matrix, but no, it’s a vector of vector. Fine. How do I make it’s transpose?

I generate the Vector{Vector} from list comprehension  
`tab = [map( (x) -> d[1] + (d[2]-d[1] )*x , rand(Uniform(0,1),Np) ) for d in domains ]`

For `Np=2` and a 5D domain `domains=( (100,1000), (-1,1), (-1,1), (-1,1), (-1,1) )` I get a

`size(tab)=(5,)`

I need to make it become a (5,2).

I have tried to use `cat`, but it’s not the right tool. `transpose` gives me a 1x5 and I cannot manipulate it to a 2D Matrix (2,5).

Any suggestion? Must be a common roadbloack for someone coming from numpy…

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [April 16, 2025, 7:42pm UTC](https://discourse.julialang.org/t/get-the-transpose-of-vector-vector-float64/128141/2 "2025-04-16T19:42:41Z")

</div>

[`stack`](https://docs.julialang.org/en/v1/base/arrays/#Base.stack)

---

<div class="post-metadata">

### Author: ![15et7dtwq](https://avatars.discourse-cdn.com/v4/letter/1/a8b319/32.png) [@15et7dtwq](https://discourse.julialang.org/u/15et7dtwq)
#### Post date: [April 16, 2025, 7:50pm UTC](https://discourse.julialang.org/t/get-the-transpose-of-vector-vector-float64/128141/3 "2025-04-16T19:50:12Z")

</div>

I figured `reduce(hcat, tab)` also works. What’s the difference with `stack`?

---

<div class="post-metadata">

### Author: ![danielwe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielwe/32/35657_2.png) [@danielwe](https://discourse.julialang.org/u/danielwe)
#### Post date: [April 16, 2025, 8:04pm UTC](https://discourse.julialang.org/t/get-the-transpose-of-vector-vector-float64/128141/4 "2025-04-16T20:04:08Z")

</div>

> [@15et7dtwq](#):
>
> `tab = [map( (x) -> d[1] + (d[2]-d[1] )*x , rand(Uniform(0,1),Np) ) for d in domains ]`

Here’s how you can both simplify this and tweak it to return a matrix:

```julia
tab_matrix = stack(rand(Uniform(d[1], d[2]), Np) for d in domains)

```

* * *

> [@15et7dtwq](#):
>
> I figured `reduce(hcat, tab)` also works. What’s the difference with `stack`?

In this case, the difference is that, to use `hcat`, you either need to wrap it in `reduce` as you did, or splat the argument as follows: `hcat(tab...)`. Meanwhile, `stack` takes `tab` as a single argument: `stack(tab)`.

In general, `stack` takes a single iterator of arrays and stacks them along a new dimension—for example, it can take a vector of matrices and stack them to form a 3D array. `hcat` takes multiple vectors/matrices and concatenates them along the second (column) dimension. For example, `hcat([1 2; 3 4], [5; 6]) == [1 2 5; 3 4 6]`.

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [April 16, 2025, 9:38pm UTC](https://discourse.julialang.org/t/get-the-transpose-of-vector-vector-float64/128141/5 "2025-04-16T21:38:05Z")

</div>

More simply, you can use `tab = [rand(Uniform(d[1], d[2])) for d in domains, _ in 1:Np]`. Or you can generate it transposed from the beginning by switching the dimensions `tab_transposed = [rand(Uniform(d[1], d[2])) for _ in 1:Np, d in domains]`.

---

<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: [April 16, 2025, 10:10pm UTC](https://discourse.julialang.org/t/get-the-transpose-of-vector-vector-float64/128141/6 "2025-04-16T22:10:55Z")

</div>

Yes, it seems unnecessary and inefficient to first generate a vector of vectors, only to `stack` or `cat` it. Just directly use a 2D array (matrix) comprehension that avoids all intermediates.

---

<div class="post-metadata">

### Author: ![danielwe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielwe/32/35657_2.png) [@danielwe](https://discourse.julialang.org/u/danielwe)
#### Post date: [April 16, 2025, 10:53pm UTC](https://discourse.julialang.org/t/get-the-transpose-of-vector-vector-float64/128141/7 "2025-04-16T22:53:12Z")

</div>

To quantify that:

```julia-repl
julia> using BenchmarkTools, Distributions

julia> domains = ((100, 1000), (-1, 1), (-1, 1), (-1, 1), (-1, 1));

julia> Np = 2;

julia> @btime stack(rand(Uniform(d...), $Np) for d in $domains);
  101.438 ns (6 allocations: 544 bytes)

julia> @btime [rand(Uniform(d...)) for _ in 1:$Np, d in $domains];
  27.010 ns (1 allocation: 144 bytes)

```

So the 2D array comprehension is ~4x faster in addition to avoiding the 5 intermediate allocations

---

<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: [April 17, 2025, 7:11pm UTC](https://discourse.julialang.org/t/get-the-transpose-of-vector-vector-float64/128141/8 "2025-04-17T19:11:38Z")

</div>

perhaps this was the form to use to directly obtain a matrix

```julia
[d[1] + (d[2]-d[1] )*x for x in rand(2), d in domains ]

```

or this

```julia
[d[1] + (d[2]-d[1] )*x for d in domains, x in rand(2) ]

```

---

<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: [April 17, 2025, 7:43pm UTC](https://discourse.julialang.org/t/get-the-transpose-of-vector-vector-float64/128141/9 "2025-04-17T19:43:45Z")

</div>

> [@DNF](#):
>
> Yes, it seems unnecessary and inefficient to first generate a vector of vectors, only to `stack` or `cat` it. Just directly use a 2D array (matrix) comprehension that avoids all intermediates.

Or better yet, re-think your whole approach to avoid creating the matrix in the first place. The biggest habit to unlearn, coming from numpy, is the urge to “vectorize” everything into a sequence of steps that generate a bunch of intermediate arrays from relatively tiny calculations (e.g. adding two vectors). Better to avoid calculating the intermediate arrays, and instead fuse the calculations into a single pass. Loops are fast in Julia. See [why vectorized code is not as fast as it could be](https://julialang.org/blog/2017/01/moredots/#why_vectorized_code_is_not_as_fast_as_it_could_be).

---

<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: [April 17, 2025, 8:36pm UTC](https://discourse.julialang.org/t/get-the-transpose-of-vector-vector-float64/128141/10 "2025-04-17T20:36:50Z")

</div>

The following is also a readable option. Define:

```julia
@inline interp(λ, rng) = λ*rng[1]+(1-λ)*rng[2]

```

Then use it (have `domains` and `Np` as in OP):

```julia
julia> interp.(rand(size(domains, 1), Np), domains)
5×2 Matrix{Float64}:
 148.587 155.122
   0.118355 0.928856
  -0.0124529 -0.694386
  -0.638283 -0.260207
   0.694686 -0.929882

julia> @btime interp.(rand(size($domains, 1), $Np), $domains);
  63.291 ns (4 allocations: 320 bytes)

```

Perhaps `interp` is defined somewhere else as it is quite basic.
