# Construct a 2-d column array

**URL:** <https://discourse.julialang.org/t/construct-a-2-d-column-array/30617>\
**Category:** General Usage\
**Tags:** question, array\
**Created:** [November 2, 2019, 12:22pm UTC](https://discourse.julialang.org/t/construct-a-2-d-column-array/30617 "2019-11-02T12:22:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![4aHxKzD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/4ahxkzd/32/10833_2.png) [@4aHxKzD](https://discourse.julialang.org/u/4aHxKzD)\
**Post date:** [November 2, 2019, 12:22pm UTC](https://discourse.julialang.org/t/construct-a-2-d-column-array/30617/1 "2019-11-02T12:22:53Z")

</div>

Is there a quick way to create an `3x1 Array{ T , 2}` where T is a type ?

`[A, B, C, ...]` or `[A; B; C; ...]` construct a 1-d array (vector).

`[A B C ...]` constructs a 2-d row array.

What about a 2-d column array? Does anyone know why `[A; B; C; ...]` wasn’t chosen for this task?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 2, 2019, 12:37pm UTC](https://discourse.julialang.org/t/construct-a-2-d-column-array/30617/2 "2019-11-02T12:37:30Z")

</div>

See this discussion for multiple suggestions (depending on whether you want to construct from existing contents, etc):

> [@How to force single column array?](https://discourse.julialang.org/t/how-to-force-single-column-array/26746):
>
> Hey guys, I want to make an array with only a single column, so ie. something like 4x1 Array{Int32,1} Whenever I use zeros or undef I get like this: zeros(Int32,4,1) 4×1 Array{Int32,2}: 0 0 0 0 Can I force the output to be a single array? Please note I know that it is possible to do: zeros(Int32,(4,)) 4-element Array{Int32,1}: 0 0 0 0 But I cannot use this notation since I am reading some data and depending on the datatype it might have 3 or 1 columns, but I do not know number of el…

---

<div class="post-metadata">

**Author:** ![4aHxKzD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/4ahxkzd/32/10833_2.png) [@4aHxKzD](https://discourse.julialang.org/u/4aHxKzD)\
**Post date:** [November 3, 2019, 4:32pm UTC](https://discourse.julialang.org/t/construct-a-2-d-column-array/30617/3 "2019-11-03T16:32:14Z")

</div>

I guess `reshape` is the way to go, but intuitively I think `[A; B; C]` makes a lot of sense and would also remove the issue of two different syntaxes doing the same thing.

---

<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:** [November 3, 2019, 4:43pm UTC](https://discourse.julialang.org/t/construct-a-2-d-column-array/30617/4 "2019-11-03T16:43:05Z")

</div>

> [@4aHxKzD](#):
>
> the issue of two different syntaxes doing the same thing.

`[A, B]` and `[A; B]` don’t do the same thing, though:

```julia
julia> A = [1, 2, 3]; B = [4, 5];

julia> [A, B]
2-element Array{Array{Int64,1},1}:
 [1, 2, 3]
 [4, 5]   

julia> [A; B]
5-element Array{Int64,1}:
 1
 2
 3
 4
 5

```

---

<div class="post-metadata">

**Author:** ![4aHxKzD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/4ahxkzd/32/10833_2.png) [@4aHxKzD](https://discourse.julialang.org/u/4aHxKzD)\
**Post date:** [November 3, 2019, 4:49pm UTC](https://discourse.julialang.org/t/construct-a-2-d-column-array/30617/5 "2019-11-03T16:49:04Z")

</div>

Didn’t think about that, thanks.

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [November 3, 2019, 10:49pm UTC](https://discourse.julialang.org/t/construct-a-2-d-column-array/30617/6 "2019-11-03T22:49:24Z")

</div>

I think that the cheapest solution, with the same cost as creating a vector, is `hvcat`:

```julia-repl
julia> using BenchmarkTools

julia> @btime [1,2,3]
  34.418 ns (1 allocation: 112 bytes)
3-element Array{Int64,1}:
 1
 2
 3

julia> @btime reshape([1,2,3],:,1)
  101.557 ns (3 allocations: 208 bytes)
3×1 Array{Int64,2}:
 1
 2
 3

julia> @btime hvcat(1, 1,2,3)
  46.672 ns (1 allocation: 112 bytes)
3×1 Array{Int64,2}:
 1
 2
 3

```

It may be a bit annoying having to put that `1` as first argument, but easy to work around it, wrapping it in another function, e.g.

```julia
columnmatrix(args...) = hvcat(1, args...)

```

---

<div class="post-metadata">

**Author:** ![4aHxKzD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/4ahxkzd/32/10833_2.png) [@4aHxKzD](https://discourse.julialang.org/u/4aHxKzD)\
**Post date:** [June 23, 2021, 2:51pm UTC](https://discourse.julialang.org/t/construct-a-2-d-column-array/30617/7 "2021-06-23T14:51:45Z")

</div>

Julia 1.7 allows

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

```
