# How to concatenate vectors to be a matrix, not a vector

**URL:** https://discourse.julialang.org/t/how-to-concatenate-vectors-to-be-a-matrix-not-a-vector/74726
**Category:** New to Julia
**Tags:** strings, array
**Created:** [January 17, 2022, 1:56am UTC](https://discourse.julialang.org/t/how-to-concatenate-vectors-to-be-a-matrix-not-a-vector/74726 "2022-01-17T01:56:19Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Xiao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiao/32/29220_2.png) [@Xiao](https://discourse.julialang.org/u/Xiao)
#### Post date: [January 17, 2022, 1:56am UTC](https://discourse.julialang.org/t/how-to-concatenate-vectors-to-be-a-matrix-not-a-vector/74726/1 "2022-01-17T01:56:19Z")

</div>

Hi, I tried a few solutions(vcat, hcat, [;], [,], etc), but haven’t got what I want.

```julia
julia> function minesweeper(input)
           allarr = []
           for line in input
               arr = split(line, "")
               allarr = [allarr; arr]
           end
           println(allarr)
       end
minesweeper (generic function with 1 method)

julia> minefield = ["123",
                    "456",
                    "789"]
3-element Vector{String}:
 "123"
 "456"
 "789"

julia> minesweeper(minefield)
Any["1", "2", "3", "4", "5", "6", "7", "8", "9"]

```

What I’d like to have is:  
[[“1”, “2”, “3”],  
[“4”, “5”, “6”],  
[“7”, “8”, “9”]]

Thank you!

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [January 17, 2022, 1:58am UTC](https://discourse.julialang.org/t/how-to-concatenate-vectors-to-be-a-matrix-not-a-vector/74726/2 "2022-01-17T01:58:12Z")

</div>

```julia
push!(allarr,arr)

```

ps: use `allarr = Vector{String}[]` so you don’t get a container of type `Any`.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [January 17, 2022, 2:13am UTC](https://discourse.julialang.org/t/how-to-concatenate-vectors-to-be-a-matrix-not-a-vector/74726/3 "2022-01-17T02:13:37Z")

</div>

> [@Xiao](#):
>
> [[“1”, “2”, “3”],  
> [“4”, “5”, “6”],  
> [“7”, “8”, “9”]]

this is not a matrix, this is a vector of vector

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [January 17, 2022, 2:18am UTC](https://discourse.julialang.org/t/how-to-concatenate-vectors-to-be-a-matrix-not-a-vector/74726/4 "2022-01-17T02:18:08Z")

</div>

You can do a comprehension and it will infer the type correctly.

```julia
julia> [split(line, "") for line in minefield]
3-element Vector{Vector{SubString{String}}}:
 ["1", "2", "3"]
 ["4", "5", "6"]
 ["7", "8", "9"]

```

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [January 17, 2022, 2:57am UTC](https://discourse.julialang.org/t/how-to-concatenate-vectors-to-be-a-matrix-not-a-vector/74726/5 "2022-01-17T02:57:20Z")

</div>

Or simply,

```julia
julia> split.(minefield, "")
3-element Vector{Vector{SubString{String}}}:
 ["1", "2", "3"]
 ["4", "5", "6"]
 ["7", "8", "9"]

```

But if lines are gauranteed to have the same length, this will be much faster:

```julia
julia> [[string(minefield[i][j]) for i=1:3] for j=1:3]
3-element Vector{Vector{String}}:
 ["1", "4", "7"]
 ["2", "5", "8"]
 ["3", "6", "9"]

```

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [January 17, 2022, 6:21am UTC](https://discourse.julialang.org/t/how-to-concatenate-vectors-to-be-a-matrix-not-a-vector/74726/6 "2022-01-17T06:21:23Z")

</div>

It’s there a reason you’re using a `Vector{Vector{String}}` (or technically `Vector{Vector{Any}}`)? This seems like a good use case for a `Matrix{Int}` from the example.

---

<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: [January 17, 2022, 6:56am UTC](https://discourse.julialang.org/t/how-to-concatenate-vectors-to-be-a-matrix-not-a-vector/74726/7 "2022-01-17T06:56:20Z")

</div>

Or as only chars are being considered here, one could simply do:

```julia
collect.(minefield)

3-element Vector{Vector{Char}}:
 ['1', '2', '3']
 ['4', '5', '6']
 ['7', '8', '9']

```

---

<div class="post-metadata">

### Author: ![Jollywatt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jollywatt/32/202198_2.png) [@Jollywatt](https://discourse.julialang.org/u/Jollywatt)
#### Post date: [January 20, 2022, 1:12am UTC](https://discourse.julialang.org/t/how-to-concatenate-vectors-to-be-a-matrix-not-a-vector/74726/8 "2022-01-20T01:12:33Z")

</div>

Answering your actual question, to concatenate vectors into a matrix, you can use [matrix literals](https://docs.julialang.org/en/v1/manual/arrays/#man-array-literals):

```julia
julia> u = [1, 2, 3]; v = collect("abc");

julia> [u v]
3×2 Array{Any,2}:
 1 'a'
 2 'b'
 3 'c'

```

This works because matrix literals have _spaces_ to separate elements horizontally, and semicolons to separate rows:

```julia
julia> [1 2; 3 4]
2×2 Array{Int64,2}:
 1 2
 3 4

```

* * *

If you want your code to use this, you’ll notice that, since `allarr = []` has zero columns, the first iteration runs into a dimension-mismatch problem.  
You can solve this by initially making `allarr` an empty `3×0` matrix (yes, that’s different to `[]`!) like so: `fill('A', 3, 0)` or `Matrix{Char}(undef, 3, 0)`.

```julia
function minesweeper(input)
    allarr = Matrix{Char}(undef, 3, 0) # initialise 3×0 matrix of `Char`s with arbitrary `undef`ed entries
    for line in input
        arr = collect(line)
        allarr = [allarr arr]
    end
    permutedims(allarr)
end

```

Notice I used `collect(line)` instead of `split(line, "")` to give a `Vector{Char}` and transposed the final matrix with `permutedims` to match the input.

This produces a proper `Matrix` (aka 2-dimensional `Array`), and _not_ a vector of vectors.
