# Easy way to merge a vector into a matrix

**URL:** https://discourse.julialang.org/t/easy-way-to-merge-a-vector-into-a-matrix/62066
**Category:** New to Julia
**Created:** [May 30, 2021, 12:59am UTC](https://discourse.julialang.org/t/easy-way-to-merge-a-vector-into-a-matrix/62066 "2021-05-30T00:59:13Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![samerb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samerb/32/9242_2.png) [@samerb](https://discourse.julialang.org/u/samerb)
#### Post date: [May 30, 2021, 12:59am UTC](https://discourse.julialang.org/t/easy-way-to-merge-a-vector-into-a-matrix/62066/1 "2021-05-30T00:59:13Z")

</div>

If I have a matrix and a separate column vector, what is the easiest way to merge that column into the matrix? Typically at the front, but potentially at an arbitrary position?

This is what I came up with. I had to do trial and error with adjoints more than seems desirable but it did work. Is there a simpler way? And what if I wanted the new column to go in the interior of the matrix rather than at the front?

```julia
data = rand(4,2)
X = vcat(ones(4)', data')'

```

Not sure what that transpose is coming up red here. It didn’t do that in Jupyter-- guess its just syntax highlighting getting confused

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [May 30, 2021, 1:19am UTC](https://discourse.julialang.org/t/easy-way-to-merge-a-vector-into-a-matrix/62066/2 "2021-05-30T01:19:14Z")

</div>

Here’s one way:

```julia
julia> let data = rand(4, 3), v = ones(4)
           [v data]
       end
4×4 Matrix{Float64}:
 1.0 0.0234619 0.965384 0.129006
 1.0 0.0234731 0.208507 0.793128
 1.0 0.637218 0.789261 0.306283
 1.0 0.629813 0.235462 0.924623

```

Here’s it stuck in the middle:

```julia
julia> let data = rand(4, 3), v = ones(4)
           [view(data, :, 1) v view(data, :, 2:3)]
       end
4×4 Matrix{Float64}:
 0.912429 1.0 0.0394902 0.888962
 0.578466 1.0 0.530733 0.152062
 0.0668675 1.0 0.265886 0.752755
 0.943337 1.0 0.962361 0.594233

```

Now for treating it as a row:

```julia
julia> let data = rand(4, 3), v = ones(1, 3)
           [v; data]
       end
5×3 Matrix{Float64}:
 1.0 1.0 1.0
 0.238316 0.845964 0.92684
 0.0655255 0.218186 0.467829
 0.925502 0.86406 0.418347
 0.606538 0.0259459 0.568094

julia> let data = rand(4, 3), v = ones(1, 3)
           [view(data, 1:2, :); v; view(data, 3:4, :)]
       end
5×3 Matrix{Float64}:
 0.149653 0.308625 0.0899298
 0.36977 0.664685 0.289565
 1.0 1.0 1.0
 0.984703 0.0198997 0.942119
 0.856835 0.632953 0.902631

```

In version 1.7, there’ll even be nict syntax to do this with N dimensional arrays: [Syntax for multidimensional arrays by BioTurboNick · Pull Request #33697 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/33697)

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [May 30, 2021, 1:26am UTC](https://discourse.julialang.org/t/easy-way-to-merge-a-vector-into-a-matrix/62066/3 "2021-05-30T01:26:47Z")

</div>

> [@samerb](#):
>
> `X = vcat(ones(4)', data')'`

Oh, I should also mention, this is equivalent to `hcat`:

```julia
julia> let data = rand(4, 3)
           vcat(ones(4)', data')' == hcat(ones(4), data)
       end
true

```
