# How do I create a 1,2,3 3x1 Matrix?

**URL:** <https://discourse.julialang.org/t/how-do-i-create-a-1-2-3-3x1-matrix/60855>\
**Category:** New to Julia\
**Created:** [May 10, 2021, 7:25am UTC](https://discourse.julialang.org/t/how-do-i-create-a-1-2-3-3x1-matrix/60855 "2021-05-10T07:25:55Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [May 10, 2021, 7:25am UTC](https://discourse.julialang.org/t/how-do-i-create-a-1-2-3-3x1-matrix/60855/1 "2021-05-10T07:25:56Z")

</div>

While introducing Arrays, I want to explain the difference in Julia between a normal column vector and a matrix with a single column… Is there a way to create a one-column matrix like `b = collect([1 2 3]')` in a simpler way (and without using rand, zeros, the Array constructor with undef/nothing…) ?

---

<div class="post-metadata">

**Author:** ![qsong](https://avatars.discourse-cdn.com/v4/letter/q/d07c76/32.png) [@qsong](https://discourse.julialang.org/u/qsong)\
**Post date:** [May 10, 2021, 7:30am UTC](https://discourse.julialang.org/t/how-do-i-create-a-1-2-3-3x1-matrix/60855/2 "2021-05-10T07:30:45Z")

</div>

```julia
reshape([1:3;],3,1)

```

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [May 10, 2021, 7:40am UTC](https://discourse.julialang.org/t/how-do-i-create-a-1-2-3-3x1-matrix/60855/3 "2021-05-10T07:40:20Z")

</div>

Or even

```julia
reshape([1:3;], :, 1)

```

if you do not want to care about number of elements.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [May 10, 2021, 7:43am UTC](https://discourse.julialang.org/t/how-do-i-create-a-1-2-3-3x1-matrix/60855/4 "2021-05-10T07:43:06Z")

</div>

There is no way to create it with just using the brackets syntax without introducing other functions, including reshape ?

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [May 10, 2021, 8:04am UTC](https://discourse.julialang.org/t/how-do-i-create-a-1-2-3-3x1-matrix/60855/5 "2021-05-10T08:04:24Z")

</div>

`[i for i=1:3, j=1:1]` but it is not compact.

---

<div class="post-metadata">

**Author:** ![qsong](https://avatars.discourse-cdn.com/v4/letter/q/d07c76/32.png) [@qsong](https://discourse.julialang.org/u/qsong)\
**Post date:** [May 10, 2021, 8:05am UTC](https://discourse.julialang.org/t/how-do-i-create-a-1-2-3-3x1-matrix/60855/6 "2021-05-10T08:05:05Z")

</div>

FYI, probably still not what you looking for though.

```julia
[1:3 1:3][:, [1]]

```

---

<div class="post-metadata">

**Author:** ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)\
**Post date:** [May 10, 2021, 8:06am UTC](https://discourse.julialang.org/t/how-do-i-create-a-1-2-3-3x1-matrix/60855/7 "2021-05-10T08:06:56Z")

</div>

`Matrix{Float64}(undef, 3, 1)`  
Does that work for you?

---

<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:** [May 10, 2021, 8:09am UTC](https://discourse.julialang.org/t/how-do-i-create-a-1-2-3-3x1-matrix/60855/8 "2021-05-10T08:09:12Z")

</div>

normally I write `hcat(1:3)`

---

<div class="post-metadata">

**Author:** ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)\
**Post date:** [May 10, 2021, 8:09am UTC](https://discourse.julialang.org/t/how-do-i-create-a-1-2-3-3x1-matrix/60855/9 "2021-05-10T08:09:39Z")

</div>

Or maybe `fill(0,(3,1))`.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [May 10, 2021, 8:10am UTC](https://discourse.julialang.org/t/how-do-i-create-a-1-2-3-3x1-matrix/60855/10 "2021-05-10T08:10:11Z")

</div>

yeah, thanks everyone, I think the answer is that it is not possible to use some simple `a = [1 ; 2 ; 3 ;]` like syntax to create a one-column matrix, as it is reinterpreted as a column vector… I’ll use one of the methods you posted, it is just that they all use already some other concepts…

---

<div class="post-metadata">

**Author:** ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)\
**Post date:** [May 10, 2021, 8:10am UTC](https://discourse.julialang.org/t/how-do-i-create-a-1-2-3-3x1-matrix/60855/11 "2021-05-10T08:10:58Z")

</div>

`a=[1 2 3]'`

---

<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:** [May 10, 2021, 8:11am UTC](https://discourse.julialang.org/t/how-do-i-create-a-1-2-3-3x1-matrix/60855/12 "2021-05-10T08:11:12Z")

</div>

```julia
[1, 2, 3][:,:]
3×1 Matrix{Int64}:
 1
 2
 3

```

And if @DNF was online, he would likely suggest:

```julia
permutedims([1 2 3])

```

---

<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:** [May 10, 2021, 8:12am UTC](https://discourse.julialang.org/t/how-do-i-create-a-1-2-3-3x1-matrix/60855/13 "2021-05-10T08:12:53Z")

</div>

The limitation is that in bracket notation a space is used to indicate `hcat` and it is ignored if you have only one argument passed.

---

<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:** [May 10, 2021, 8:30am UTC](https://discourse.julialang.org/t/how-do-i-create-a-1-2-3-3x1-matrix/60855/14 "2021-05-10T08:30:58Z")

</div>

One wouldn’t recommend this, but it is interesting:

```julia
using LinearAlgebra
julia> [1, 2, 3]*I
3×1 Matrix{Int64}:
 1
 2
 3

```

---

<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:** [May 10, 2021, 9:05am UTC](https://discourse.julialang.org/t/how-do-i-create-a-1-2-3-3x1-matrix/60855/15 "2021-05-10T09:05:43Z")

</div>

> [@sylvaticus](#):
>
> I think the answer is that it is not possible to use some simple `a = [1 ; 2 ; 3 ;]` like syntax to create a one-column matrix, as it is reinterpreted as a column vector…

Note that there is a PR [Syntax for multidimensional arrays by BioTurboNick · Pull Request #33697 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/33697), which, if I understand it correctly, means that `a = [1 ; 2 ; 3 ;;]` may be a future syntax for this. Specifically see the proposed [`NEWS`](https://github.com/JuliaLang/julia/pull/33697/files#diff-51920e95310ebfbc1ae31709f3b95f89afffbf4f1a6e38e8b2b406e2fb6197ea) entry.

---

<div class="post-metadata">

**Author:** ![Jake](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jake/32/46007_2.png) [@Jake](https://discourse.julialang.org/u/Jake)\
**Post date:** [May 10, 2021, 1:15pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-1-2-3-3x1-matrix/60855/16 "2021-05-10T13:15:32Z")

</div>

> [@sylvaticus](#):
>
> While introducing Arrays, I want to explain the difference in Julia between a normal column vector and a matrix with a single column

I thought a Vector was a synonym for a 1 Column Matrix? Why would there be a need to differentiate?

---

<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:** [May 10, 2021, 1:27pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-1-2-3-3x1-matrix/60855/17 "2021-05-10T13:27:30Z")

</div>

There are many reasons, e.g.:

- they have different `ndims`
- you can resize `Vector`, but you cannot resize `Matrix`
- there are functions that accept only `Vector` or only `Matrix` (especially in `LinearAlgebra`)
- Tables.jl tables usually require `AbstractVector`, but disallow 1-column `AbstractMatrix` as columns

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [May 10, 2021, 2:35pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-1-2-3-3x1-matrix/60855/18 "2021-05-10T14:35:19Z")

</div>

Mathematically they are the same, but they are different objects in Julia. `[1,2,3]` is a uni-dimensional array, `collect([1 2 3]')` is a two- dimensional array where “it happens” that the second dimension has size 1, i.e. there is only one column. So, if a function requires `AbstractArray{T,1}` the first will fit, but the second will not.  
Note that this possible confusion doesn’t arise with _row_ vectors, as all row vectors in Julia have 2 dimensions.

> **[2 - Data types](https://syl1.gitbook.io/julia-language-a-concise-tutorial/language-core/data-types#multidimensional-and-nested-arrays)**

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [May 14, 2021, 9:26am UTC](https://discourse.julialang.org/t/how-do-i-create-a-1-2-3-3x1-matrix/60855/19 "2021-05-14T09:26:37Z")

</div>

You can also use

```julia
Matrix([1,2,3]')

```

if that’s clearer than `collect`

---

<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:** [May 14, 2021, 10:31am UTC](https://discourse.julialang.org/t/how-do-i-create-a-1-2-3-3x1-matrix/60855/20 "2021-05-14T10:31:31Z")

</div>

@tomerarnon, you probably meant:

```julia
Matrix([1 2 3]')
3×1 Matrix{Int64}:

```

[Next page](https://discourse.julialang.org/t/how-do-i-create-a-1-2-3-3x1-matrix/60855.md?page=2)
