# How to create a 2x1 Array?

**URL:** <https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337>\
**Category:** New to Julia\
**Created:** [January 13, 2018, 5:51am UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337 "2018-01-13T05:51:40Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [January 15, 2018, 7:56pm UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/21 "2018-01-15T19:56:15Z")

</div>

This seams to be simple to me:

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

```

Does it not work any longer on 0.7?

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [January 15, 2018, 8:06pm UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/22 "2018-01-15T20:06:32Z")

</div>

It works, but it creates:

```julia
julia> [1 2]'
2×1 Adjoint{Int64,Array{Int64,2}}:
 1
 2

```

so it is a lazy adjoint.

On 0.7 you can use either:

```julia
permutedims([1 2])

```

or as I have noted above (which is a bit shorter and faster):

```julia
 hvcat(1, 1, 2)

```

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [January 15, 2018, 8:08pm UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/23 "2018-01-15T20:08:22Z")

</div>

Thats bad. But what is the difference between an Adjoint and an array in the first place?

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [January 15, 2018, 8:14pm UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/24 "2018-01-15T20:14:55Z")

</div>

Here you can see two differences:

```julia
julia> x = [1im, 2im]
2-element Array{Complex{Int64},1}:
 0 + 1im
 0 + 2im

julia> y = x'
1×2 Adjoint{Complex{Int64},Array{Complex{Int64},1}}:
 0-1im 0-2im

julia> x[1,1] = 100
100

julia> x
2-element Array{Complex{Int64},1}:
 100 + 0im
   0 + 2im

julia> y
1×2 Adjoint{Complex{Int64},Array{Complex{Int64},1}}:
 100+0im 0-2im

julia> y[1,1]=10
10

julia> y
1×2 Adjoint{Complex{Int64},Array{Complex{Int64},1}}:
 10+0im 0-2im

julia> x
2-element Array{Complex{Int64},1}:
 10 + 0im
  0 + 2im

```

First - if you use complex numbers there is a difference. Second - it is a view of the original array.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [January 15, 2018, 8:16pm UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/25 "2018-01-15T20:16:30Z")

</div>

Also the benefit of `hvcat` is that it works on 0.7 and 0.6.2.

---

<div class="post-metadata">

**Author:** ![JoshLangsfeld](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joshlangsfeld/32/8211_2.png) [@JoshLangsfeld](https://discourse.julialang.org/u/JoshLangsfeld)\
**Post date:** [January 15, 2018, 8:20pm UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/26 "2018-01-15T20:20:57Z")

</div>

No one has mentioned macros, but it’s trivial to write one for 2D array construction. Example:

```julia
julia> macro matrix(array)
         rows = [Expr(:row, val) for val in array.args]
         Expr(:vcat, rows...)
       end
@matrix (macro with 1 method)

julia> @matrix [1,2,3]
3×1 Array{Int64,2}:
 1
 2
 3

```

---

<div class="post-metadata">

**Author:** ![favba](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/favba/32/2735_2.png) [@favba](https://discourse.julialang.org/u/favba)\
**Post date:** [January 15, 2018, 8:52pm UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/27 "2018-01-15T20:52:53Z")

</div>

This issue is related to the topic:  
[https://github.com/JuliaLang/julia/issues/7128](https://github.com/JuliaLang/julia/issues/7128)

It seems there is no easy way to do this because the syntax `[a;b]` is reserved for array concatenation.  
I agree with @Liso , the solutions proposed here are incredibly complicated for such simple task.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [January 15, 2018, 8:53pm UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/28 "2018-01-15T20:53:06Z")

</div>

Nice macro. The only drawback is that you have to interpolate if you want to pass a variable like `@matrix $x`.

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [January 15, 2018, 10:27pm UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/29 "2018-01-15T22:27:46Z")

</div>

> [@bkamins](#):
>
> First - if you use complex numbers are is a difference.

This problem is because it is recursive operation. And it has impact not only to complex number:

```julia
# 0.6.1
julia> ["a" "b"]'
ERROR: MethodError: no method matching transpose(::String)

#0.7.0
julia> ["a" "b"]'
2×1 Adjoint{Any,Array{String,2}}:
Error showing value of type Adjoint{Any,Array{String,2}}:
ERROR: MethodError: no method matching adjoint(::String)

```

You could try to do it for Array of Arrays…

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [January 15, 2018, 10:42pm UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/30 "2018-01-15T22:42:17Z")

</div>

Agreed - that is why the documentation for `'` states

> This operation is intended for linear algebra usage

I guess.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [January 16, 2018, 9:03am UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/31 "2018-01-16T09:03:49Z")

</div>

Slightly shorter option that works on both 0.6 and 0.7.

```julia
julia> hcat([1, 2])
2×1 Array{Int64,2}:
 1
 2

```

---

<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:** [January 16, 2018, 10:11am UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/32 "2018-01-16T10:11:57Z")

</div>

Reviewing the many solutions in this thread, it is apparent that while there are many ways to create single-column matrices, there is no special syntax for this.

I wonder if this is really a problem. Many types in `Base` has no short syntax, they are just constructed with a constructor or a function for this purpose. I can imagine three possibilities:

1. Single-column matrices are not used frequently, eg because vectors are columns vectors in linear algebra, so they are rarely needed. When needed (eg for testing), there are workarounds. If a package uses a lot of these for some reason, they can define a function for it.

2. They are used frequently enough that a constructor syntax is warranted in base. We can either

I am leaning towards 1, but perhaps I just have different use cases.

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [January 16, 2018, 10:44am UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/33 "2018-01-16T10:44:33Z")

</div>

Tbh I’m still in favour of

```julia
[1, 2, 3] -> Vector
[1 2 3] -> 1x3 Matrix
[1; 2; 3] -> 3x1 Matrix

```

This seems like the only consistent option for literal syntaxes to me, but I can’t say I really care all that much 🙂

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [January 16, 2018, 11:40am UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/34 "2018-01-16T11:40:53Z")

</div>

> [@Tamas\_Papp](#):
>
> Single-column matrices are not used frequently, eg because vectors are columns vectors in linear algebra, so they are rarely needed. When needed (eg for testing), there are workarounds. If a package uses a lot of these for some reason, they can define a function for it.

FWIW, I can’t remember ever creating an `Nx1` Matrix in the past year or so and I’m almost thinking it could be better that it is hard to create them as they are so rarely useful.

If instead there actually is a need for some syntax, maybe the easiest would be to add a `Matrix(v::Vector)` method that would create an `Nx1` matrix, just in the same way as `Matrix(v::RowVector)` would create a `1xN` Matrix. On 0.6 and master it is a method error, so I guess it wouldn’t be a breaking change.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [January 16, 2018, 12:30pm UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/35 "2018-01-16T12:30:01Z")

</div>

See also [https://github.com/JuliaLang/julia/pull/23790](https://github.com/JuliaLang/julia/pull/23790).

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [January 17, 2018, 10:32am UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/36 "2018-01-17T10:32:26Z")

</div>

Part of the rationale for not having an easy way to create an N-by-1 matrix is that such matrices are usually better treated as vectors. However, the return values of expressions like `maximum(A, 2)` go against this philosophy. The returned array will always have a singleton dimension. It would seem natural to let the `maximum` function `squeeze` out this dimension. If this were the case, then the code from the original post would have worked as intended.

Of course it is sometimes useful to preserve the singleton dimension, but one could perhaps distinguish between `maximum(A, (2,))` which would `squeeze`, and `maximum(A, [2])` which keep the current behavior.

(Of course this would affect a lot of existing code, and it’s too late for new features for 1.0 anyways.)

---

<div class="post-metadata">

**Author:** ![HenrikM](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrikm/32/17975_2.png) [@HenrikM](https://discourse.julialang.org/u/HenrikM)\
**Post date:** [March 27, 2019, 1:05pm UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/37 "2019-03-27T13:05:03Z")

</div>

I found this thread while scratching my head over array creation syntax. Why does Julia 1.1.0 still have two ways to make a vector, one way to make a 1×d array and zero ways to make a d×1 array? Can this still be changed?

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

julia> [1,2]
2-element Array{Int64,1}:
 1
 2

julia> [1;2]
2-element Array{Int64,1}:
 1
 2

```

---

<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:** [March 27, 2019, 1:24pm UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/38 "2019-03-27T13:24:57Z")

</div>

> [@HenrikM](#):
>
> zero ways to make a d×1 array

This is not correct, please review the solutions above.

---

<div class="post-metadata">

**Author:** ![HenrikM](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrikm/32/17975_2.png) [@HenrikM](https://discourse.julialang.org/u/HenrikM)\
**Post date:** [March 27, 2019, 1:45pm UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/39 "2019-03-27T13:45:45Z")

</div>

Ok, sorry for not being clear on what I meant. I do use single column matrices and what I meant was:

> [@Tamas\_Papp](#):
>
> Reviewing the many solutions in this thread, it is apparent that while there are many ways to create single-column matrices, **there is no special syntax for this**.

Emphasis mine.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 27, 2019, 2:09pm UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/40 "2019-03-27T14:09:06Z")

</div>

How about a macro?

```julia
julia> using MacroTools

julia> macro col(expr)
         @capture expr [x__]
         Expr(:call, :hvcat, Expr(:tuple, [1 for _ in x]...), esc.(x)...)
       end
@col (macro with 1 method)

julia> @col [1,2]
2×1 Array{Int64,2}:
 1
 2

julia> @col [1, 2, 3]
3×1 Array{Int64,2}:
 1
 2
 3

```

[Previous page](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337.md?page=1)

[Next page](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337.md?page=3)
