# 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:** 1

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [January 13, 2018, 5:51am UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/1 "2018-01-13T05:51:41Z")

</div>

TL;DR, answer: to get a 2x1 Array/Matrix transpose a 1x2 matrix, for example: `[2 4]' `  
A more general solution that also works for complex numbers in Julia V0.7- would be `transpose([2 4])`

* * *

I wanted to test

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

```

So I tried

```julia
julia> maximum([1 2; 3 4], 2) == [2;4]
false

```

I expected the value to be true, but the value is false because

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

```

That is, a 2×1 Array{Int64,2} is not the same as a 2-element Array{Int64,1} (2D vs 1D)

However,

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

```

So [2;4] (note the semicolon) seems the proper way to get a 2×1 Array{Int64,2} 2-dimensional array.

What am I missing?

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [January 13, 2018, 6:01am UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/2 "2018-01-13T06:01:58Z")

</div>

try to use `all(a.==b)` instead of `a==b`. Don’t have a computer atm but maybe `==` is comparing the references?

`.==` is element-wise comparison

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [January 13, 2018, 6:11am UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/3 "2018-01-13T06:11:10Z")

</div>

```julia
julia> all(maximum([1 2; 3 4], 2) .== [2;4])
true

```

That works, but it doesn’t answer the question how to create a 2×1 Array{Int64,2} 2-dimensional array.

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [January 13, 2018, 6:27am UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/4 "2018-01-13T06:27:52Z")

</div>

Why not just `Matrix{Int64}(2, 1)`?

If you want it initialized, you can do `zeros(Int64, 2, 1)`

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [January 13, 2018, 6:39am UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/5 "2018-01-13T06:39:09Z")

</div>

Yes, that can be done, but it requires multiple statements. But why should the simplest case require the most work? [1 2; 3 4] gives me a 2x2 matrix. [1 2] gives me a 1x2 matrix. Why doesn’t there seem to be a simple way to create a 2x1 matrix? It turns out that [1;2] is the same as [1,2]. One would think/hope/expect that [1;2] would result in a 2x1 matrix, but it doesn’t.

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [January 13, 2018, 7:06am UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/6 "2018-01-13T07:06:02Z")

</div>

You didn’t specify that you wanted specific values in your matrix, so I didn’t provide you with that. `hcat(1,2)'` will do what you want (if you want `1` and `2` as your elements. Substitute as appropriate.)

---

<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 13, 2018, 7:14am UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/7 "2018-01-13T07:14:55Z")

</div>

The problem with `hcat(1,2)'` is that its type is `Adjoint{Int64,Array{Int64,2}}`, which is correct if you do not use complex numbers but it is simpler to write `hvcat(1, 1, 2)` I guess to get `2×1 Array{Int64,2}` directly.

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [January 13, 2018, 7:16am UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/8 "2018-01-13T07:16:37Z")

</div>

> The problem with `hcat(1,2)'` is that its type is `Adjoint{Int64,Array{Int64,2}}`…

```julia
julia> typeof(hcat(1,2)')
Array{Int64,2}

```

Perhaps in 0.7? I’m using -release.

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [January 13, 2018, 7:17am UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/9 "2018-01-13T07:17:30Z")

</div>

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

```

But I’m looking for a 2x1 matrix, not a 1x2 matrix. So I tried

```julia
julia> hvcat((1,1),2,4)
2×1 Array{Int64,2}:
 2
 4

```

and indeed

```julia
julia> maximum([1 2; 3 4], 2) == hvcat((1,1),2,4)
true

```

We have a winner! Thank you all for your suggestions.

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [January 13, 2018, 7:18am UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/10 "2018-01-13T07:18:00Z")

</div>

You’ll note that I transposed the `hcat` - see the `'` at the end?

```julia
julia> maximum([1 2; 3 4], 2) == hcat(2,4)'
true

```

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [January 13, 2018, 7:27am UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/11 "2018-01-13T07:27:29Z")

</div>

Very clever. I like hcat(2,4)’ even better than hvcat((1,1),2,4). Thanks.

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [January 13, 2018, 7:34am UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/12 "2018-01-13T07:34:16Z")

</div>

In 0.7 I use `reshape([2,4], Val{2}())`  
I think it is quiet nice.  
It works for any length array, giving a column matrix.

Not available in 0.6 though.

In 0.5 (and earlier) I used to use `[2,4]''` but that doesn’t work in 0.6 or 0.7  
because we now take vector transpose seriously.

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

---

<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 13, 2018, 7:55am UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/13 "2018-01-13T07:55:18Z")

</div>

I use

```julia
reshape([1, 2], :, 1)

```

which works in both `v0.6.2` and `v0.7-`.

---

<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:** [January 13, 2018, 8:05am UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/14 "2018-01-13T08:05:12Z")

</div>

Why use `hcat(2,4)'` instead of `[2 4]'`? The latter seems terser, nicer and more idiomatic.

---

<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 13, 2018, 9:08am UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/15 "2018-01-13T09:08:28Z")

</div>

Yes - on 0.7.

---

<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 13, 2018, 9:12am UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/16 "2018-01-13T09:12:31Z")

</div>

Also, for this particular use case (testing) you can also

```julia
squeeze(maximum([1 2; 3 4], 2), 2) == [2, 4]

```

or the slightly less elegant (because it does not catch some shape errors)

```julia
vec(maximum([1 2; 3 4], 2)) == [2, 4]

```

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [January 13, 2018, 6:58pm UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/17 "2018-01-13T18:58:01Z")

</div>

Shouldn’t it be `[1 2].'` for a general solution, otherwise complex values will be an issue.

---

<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 13, 2018, 7:20pm UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/18 "2018-01-13T19:20:58Z")

</div>

> [@j\_verzani](#):
>
> `[1 2].'`

Note that the `.'` is deprecated in `v0.7-`.

`copy(transpose([1 2]))` will be fine though.

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [January 13, 2018, 8:48pm UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/19 "2018-01-13T20:48:25Z")

</div>

Ooops, thanks for the clarification and pointing out that `transpose` is the underlying function to call, not `ctranspose/adjoint`.

---

<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, 8:57am UTC](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/20 "2018-01-15T08:57:41Z")

</div>

> [@jandehaan](#):
>
> Why doesn’t there seem to be a simple way to create a 2x1 matrix?

It is probably some kind of [stockholm syndrom](https://en.wikipedia.org/wiki/Stockholm_syndrome) when we develop psychological alliance with pseudo-solutions like `reshape`, `hcat`, `transpose`, etc.

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