# 1-Column matrix vs. Vector

**URL:** <https://discourse.julialang.org/t/1-column-matrix-vs-vector/71440>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [November 13, 2021, 10:28pm UTC](https://discourse.julialang.org/t/1-column-matrix-vs-vector/71440 "2021-11-13T22:28:39Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [November 13, 2021, 10:28pm UTC](https://discourse.julialang.org/t/1-column-matrix-vs-vector/71440/1 "2021-11-13T22:28:39Z")

</div>

Here is a real world example:

```julia
a = Float64[-159.5; -158.5; -39.5;;]
typeof(a)
Matrix{Float64} (alias for Array{Float64, 2})
size(a)
(3, 1)

```

```julia
b = vec(a)
b = Float64[-159.5, -158.5, -39.5]
typeof(b)
Vector{Float64} (alias for Array{Float64, 1})
size(b)
(3, )

```

It seems that Julia considers them to be different? The size of `a` is (3,1) and the size of `b` is (3,). Can anyone please explain What is causing all of this to happen? Why doesn’t Julia treat them the same?

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [November 13, 2021, 10:33pm UTC](https://discourse.julialang.org/t/1-column-matrix-vs-vector/71440/2 "2021-11-13T22:33:06Z")

</div>

```julia
a = [1; 2; 3]
b = [1, 2, 3]
println(typeof(a))
println(typeof(b))
println(a == b)

```

shows

```julia
Vector{Int64}
Vector{Int64}
true

```

I’m confused.

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [November 13, 2021, 11:19pm UTC](https://discourse.julialang.org/t/1-column-matrix-vs-vector/71440/3 "2021-11-13T23:19:54Z")

</div>

> [@leon](#):
>
> `b = Float64[1, 2, 3]`

Sorry for the confusion. Maybe that’s not a good example. Note I have modified the original post.

In my cases, when it is not working, below is often the type:  
`typeof(a) = Matrix{Float64}`

After I do an `b =vec(a)`, it will work and now its type is as below:  
`typeof(b) = Vector{Float64}`

Here is a real world example:

`a = Float64[-159.5; -158.5; -39.5;;]`  
`typeof(a)`  
Matrix{Float64} (alias for Array{Float64, 2})

`b = vec(a)`  
b = Float64[-159.5, -158.5, -39.5]  
`typeof(b)`  
Vector{Float64} (alias for Array{Float64, 1})

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [November 13, 2021, 11:35pm UTC](https://discourse.julialang.org/t/1-column-matrix-vs-vector/71440/4 "2021-11-13T23:35:14Z")

</div>

For two dimensions we have

```julia
a1 = [[1, 2], [3, 4]]
println(typeof(a1))
b1 = [[1, 2]; [3, 4]]
println(typeof(b1))

a2 = hcat([1, 2], [3, 4])
println(typeof(a2))

b2 = vcat([1, 2], [3, 4])
println(typeof(b2))
println(b2 == b1)

```

with

```julia
Vector{Vector{Int64}}
Vector{Int64}
Matrix{Int64}
Vector{Int64}
true

```

I tend to prefer the `cat`-family of functions.

---

<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:** [November 13, 2021, 11:43pm UTC](https://discourse.julialang.org/t/1-column-matrix-vs-vector/71440/5 "2021-11-13T23:43:34Z")

</div>

I found this text which probably provides the detailed motivation for the difference between one-column matrices and vectors:

[https://github.com/toivoh/Julia-Design-Thoughts/wiki/Row-vectors#motivation](https://github.com/toivoh/Julia-Design-Thoughts/wiki/Row-vectors#motivation)

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [November 13, 2021, 11:50pm UTC](https://discourse.julialang.org/t/1-column-matrix-vs-vector/71440/6 "2021-11-13T23:50:41Z")

</div>

We would need a rather hypothetical type system to equate `n x 1` matrices with column and `1 x m` matrices with row vectors. So compromises have to be made, probably.

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [November 13, 2021, 11:55pm UTC](https://discourse.julialang.org/t/1-column-matrix-vs-vector/71440/7 "2021-11-13T23:55:17Z")

</div>

Many thanks for the very helpful replies!

BTW, how can I create a size(3, 1) matrix in Julia?

I just tried. Either `,` or `;` will result in a vector with the size of (3,) directly.

```julia
b1 = [1; 2; 3] 
b2 = [1, 2, 3]

```

If I created m by n matrix called B first and do b = B[:,1], it will give me a vector as well.

---

<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:** [November 13, 2021, 11:56pm UTC](https://discourse.julialang.org/t/1-column-matrix-vs-vector/71440/8 "2021-11-13T23:56:38Z")

</div>

> [@leon](#):
>
> BTW, how can I create a size(3, 1) matrix in Julia?

I saw it in your code:

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

```

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [November 13, 2021, 11:59pm UTC](https://discourse.julialang.org/t/1-column-matrix-vs-vector/71440/9 "2021-11-13T23:59:56Z")

</div>

Many thanks!

Wow, I thought the double semicolons at the end were by accident. They were actually intentional.

---

<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:** [November 14, 2021, 12:04am UTC](https://discourse.julialang.org/t/1-column-matrix-vs-vector/71440/10 "2021-11-14T00:04:35Z")

</div>

> [@goerch](#):
>
> So compromises have to be made, probably.

Yeah… I tried to follow some GitHub issues on the original design of these, but they are pretty technical. What’s clear is that everyone involved had clear knowledge on what Matlab does, and they agreed that having that distinction is a good thing. I’ve seen also a C. Elrod post demonstrating that some compiler optimizations are easier with vectors (vs. Matrices that happen to have one column), particularly for simd.

In Matlab even a scalar is a Matrix with one row and one column. In Julia this would be very bad, since scalars are immutable types, and matrices are heap allocated. Probably something like that, yet more subtle, is related to the choice of having true vectors.

Some sources: [(Row)Vector equality with Matrices · Issue #21998 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/21998)

> [@1d arrays vs 1-column matrices](https://discourse.julialang.org/t/1d-arrays-vs-1-column-matrices/46101/8):
>
> The compiler has more information about a vector than a matrix: it knows the second dimension is 1. This can yield better performance in cases such as: julia\> x = rand(128); X = reshape(x, (length(x),1)); julia\> typeof.((x, X)) (Array{Float64,1}, Array{Float64,2}) julia\> A = rand(32,128); B = similar(A); julia\> @benchmark @. $B = $A + $x' BenchmarkTools.Trial: memory estimate: 0 bytes allocs estimate: 0 -------------- minimum time: 773.423 ns (0.00% GC) median time: 784…

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [November 14, 2021, 12:18am UTC](https://discourse.julialang.org/t/1-column-matrix-vs-vector/71440/11 "2021-11-14T00:18:25Z")

</div>

Unfortunately, this design has been a major problem to my programs.

If having data in vector is so important, what harm will it do to design their programs by first converting any applicable 1-column matrix into a vector internally?

“Elrod post demonstrating that some compiler optimizations are easier with vectors (vs. Matrices that happen to have one column), particularly for simd.”

---

<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:** [November 14, 2021, 12:24am UTC](https://discourse.julialang.org/t/1-column-matrix-vs-vector/71440/12 "2021-11-14T00:24:20Z")

</div>

> [@leon](#):
>
> Unfortunately, this design has been a major problem to my programs.

I cannot argue in any direction about the design decision. But maybe if you show how your program reaches these problems, you can get some advice on how to structure the code to avoid them.

For instance, where are these 1-column matrices coming from? (since matrix\*vector multiplications in Julia return vectors, I usually never get these).

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [November 14, 2021, 12:40am UTC](https://discourse.julialang.org/t/1-column-matrix-vs-vector/71440/13 "2021-11-14T00:40:31Z")

</div>

> [@leon](#):
>
> Unfortunately, this design has been a major problem to my programs.

I feel your frustration. OTOH is there any documentation about MATLAB’s type system (if that is what we are talking about)? I have no idea how such type identities should generalize.

---

<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:** [November 14, 2021, 12:45am UTC](https://discourse.julialang.org/t/1-column-matrix-vs-vector/71440/14 "2021-11-14T00:45:08Z")

</div>

Note that this notation is valid in 1.7. In 1.6 it produces a vector:

```julia
julia> b1 = [1; 2; 3;;]
3-element Vector{Int64}:
 1
 2
 3

julia> reshape(b1,3,1)
3×1 Matrix{Int64}:
 1
 2
 3

```

that can be reshaped to a (3,1) matrix. (I wander why that new notation in 1.7 was not considered a breaking change, if it didn’t error before)

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [November 14, 2021, 12:53am UTC](https://discourse.julialang.org/t/1-column-matrix-vs-vector/71440/15 "2021-11-14T00:53:51Z")

</div>

Thanks!

In a lot of my cases, the data were from a 3rd party and the function I use was from a 3rd party Julia package. Therefore, I did not really have a lot of control, except for trying to wrap up all 1-column data with `vec()`, or waiting till errors were reported.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [November 14, 2021, 1:29am UTC](https://discourse.julialang.org/t/1-column-matrix-vs-vector/71440/16 "2021-11-14T01:29:04Z")

</div>

Was it one specific package? If so, part of the answer might be to make a PR to change the library to return `Vector`s if that would be more appropriate.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [November 14, 2021, 1:32am UTC](https://discourse.julialang.org/t/1-column-matrix-vs-vector/71440/17 "2021-11-14T01:32:11Z")

</div>

> [@leon](#):
>
> It seems that Julia considers them to be different? The size of `a` is (3,1) and the size of `b` is (3,). Can anyone please explain What is causing all of this to happen? Why doesn’t Julia treat them the same?

I wrote a long post explaining many motivations for distinguishing these as types when you brought it up previously:

> [@My wishlist for the next version of Julia](https://discourse.julialang.org/t/my-wishlist-for-the-next-version-of-julia/69987/57):
>
> In Matlab there are no vectors, only single-column matrices. There are arrays with higher numbers of dimensions, however, so it treats two as a special number of dimensions. I get how they ended up there: early versions only had matrices, which gets you shockingly far, only later did they add higher dimensional arrays. However, that’s unsatisfying (why is two special?) and causes problems. Think of all the cases where Matlab does something special and different when a matrix happens to only have…

---

<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 14, 2021, 9:46am UTC](https://discourse.julialang.org/t/1-column-matrix-vs-vector/71440/18 "2021-11-14T09:46:09Z")

</div>

I’ve vented my frustrations with Matlab’s lack of a proper vector type several times. The post I link below is one of the posts.

In short, I use Matlab most of the time, and the fact that it does not make that distinction is one of the _worst_ qualities of Matlab. Having a proper vector type is a huge improvement, and that’s nothing to do with performance.

> [@Trouble Understanding Slicing](https://discourse.julialang.org/t/trouble-understanding-slicing/36821/7):
>
> I will leave the other questions aside, but I have to say that the absence of proper vectors in Matlab is a huge annoyance. It’s causing me endless trouble almost every time I use Matlab (which is at least 5 days a week.) You really should learn to appreciate it. The absence of vectors in Matlab means that in all code you have to make a decision and try to keep track of whether vectors are row or column vectors. Matlab pretends to be column-major, and functions like sum, prod, max, plot, etc. …

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [November 14, 2021, 2:00pm UTC](https://discourse.julialang.org/t/1-column-matrix-vs-vector/71440/19 "2021-11-14T14:00:34Z")

</div>

I think Jiahao Chen’s talk at JuliaCon 2017 on “Taking Vector Transposes Seriously” is worth watching for anyone interested in this question:

[![](https://global.discourse-cdn.com/julialang/original/3X/e/5/e55a2a411b08eba0d6e2cdb7b9392b0f4a8a5eee.jpeg "JuliaCon 2017 | Taking Vector Transposes Seriously | Jiahao Chen") ](https://www.youtube.com/watch?v=C2RO34b_oPM)

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [November 14, 2021, 4:11pm UTC](https://discourse.julialang.org/t/1-column-matrix-vs-vector/71440/20 "2021-11-14T16:11:43Z")

</div>

> [@Oscar\_Smith](#):
>
> Was it one specific package? If so, part of the answer might be to make a PR to change the library to return `Vector` s if that would be more appropriate.

I want to emphasize this. You should almost always be using `Vector`s in your code, rather than column vectors. That’s a MATLAB habit that you need to shake. I don’t think I’ve ever encountered a Julia package that uses or returns column vectors. If there is such a package, I would be interested in hearing about it.

In my opinion, the usage of the `Vector` type makes a lot of sense mathematically. Keep in mind that vectors in \mathbb{R}^n are actually n-tuples. They are _not_ column vectors. A column vector is one possible _matrix representation_ of a vector. It’s nice that Julia has different types that represent tensors of different orders. We have `Number` for 0-order tensors, `Vector` for 1st-order tensors, `Matrix` for 2nd-order tensors, `Array{T,3} where T` for 3rd-order tensors, etc.
