# Vector vs Column vector

**URL:** https://discourse.julialang.org/t/vector-vs-column-vector/34699
**Category:** New to Julia
**Tags:** question
**Created:** [February 16, 2020, 2:10am UTC](https://discourse.julialang.org/t/vector-vs-column-vector/34699 "2020-02-16T02:10:24Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![ea42gh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ea42gh/32/12000_2.png) [@ea42gh](https://discourse.julialang.org/u/ea42gh)
#### Post date: [February 16, 2020, 2:10am UTC](https://discourse.julialang.org/t/vector-vs-column-vector/34699/1 "2020-02-16T02:10:24Z")

</div>

I can’t wrap my mind around the following:

```julia
a=[2 4]; b = [3; 6]; c = a*b

```

`a` is a row vector (matrix)  
I expected b to be a column vector: it is a vector, though,  
so the computation of `c=a*b` fails.

How should I think about this? I had expected to get a matrix c of size 1x1

---

<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: [February 16, 2020, 2:26am UTC](https://discourse.julialang.org/t/vector-vs-column-vector/34699/2 "2020-02-16T02:26:15Z")

</div>

you want `dot(a,b)` (which needs a `using LinearAlgebra` to be run first. The reason this fails is that there are multiple possible products which you could mean (inner or outer).

---

<div class="post-metadata">

### Author: ![ea42gh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ea42gh/32/12000_2.png) [@ea42gh](https://discourse.julialang.org/u/ea42gh)
#### Post date: [February 16, 2020, 2:29am UTC](https://discourse.julialang.org/t/vector-vs-column-vector/34699/3 "2020-02-16T02:29:48Z")

</div>

That much I get.  
What I don’t understand is why

```julia
a = [1 2] is a matrix
while
a=[1,2],
a = [1
      2] and
a =[1;2] are vectors?

```

---

<div class="post-metadata">

### Author: ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)
#### Post date: [February 16, 2020, 8:12am UTC](https://discourse.julialang.org/t/vector-vs-column-vector/34699/4 "2020-02-16T08:12:47Z")

</div>

`[x,y]` constructs a vector of two elements, `x` and `y`. This is true even if you put an array inside:

```julia
julia> [[1,2], 3]
2-element Array{Any,1}:
  [1, 2]
 3      

```

In contrast, `;` concatenates:

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

```

It might be occasionally useful to have a convenient way of constructing a literal `n×1` matrix, and your syntax with the multi-line input is a potential way to signal that. I’m not sure this has been discussed, so if no one else chimes in you could try submitting an issue ([Issues · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues)).

> so the computation of `c=a*b` fails.

Not for me:

```julia
julia> a=[2 4]; b = [3; 6]; c = a*b
1-element Array{Int64,1}:
 30

```

That’s the sensible result: in the same way that a `m×n` array times a `n×p` array should give a `m×p` array, a `m×n` array times a `n` array should give a `m` array.

---

<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: [February 16, 2020, 8:37am UTC](https://discourse.julialang.org/t/vector-vs-column-vector/34699/5 "2020-02-16T08:37:49Z")

</div>

> [@tim.holy](#):
>
> I’m not sure this has been discussed

There are many discussions, eg

> [@Construct a 2-d column array](https://discourse.julialang.org/t/construct-a-2-d-column-array/30617):
>
> 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?

and there is

> <https://github.com/JuliaLang/julia/issues/17084>
>
> \`hvcat\` concatenation syntax produces matrices, such as in
> 
> \`\`\`
> julia\> \[1 2; 3 4…\]
> 2×2 Array{Int64,2}:
> 1 2
> 3 4
> 
> julia\> \[1 2\]
> 1×2 Array{Int64,2}:
> 1 2
> \`\`\`
> 
> But there is no corresponding way to create a single column matrix:
> 
> \`\`\`
> julia\> \[1; 3\]
> 2-element Array{Int64,1}:
> 1
> 3
> 
> julia\> \[1;\]
> 1-element Array{Int64,1}:
> 1
> \`\`\`
> 
> I suggest that the latter should invoke \`hvcat\` and not \`vcat\`, to produce matrices. This implies that there would be no special \`vcat\` syntax any more.

---

<div class="post-metadata">

### Author: ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)
#### Post date: [February 16, 2020, 12:21pm UTC](https://discourse.julialang.org/t/vector-vs-column-vector/34699/6 "2020-02-16T12:21:06Z")

</div>

My recollection on the reason there’s no convenient syntax, despite many discussions and perfectly reasonable proposals, is that there just wasn’t consensus on which to use before Julia v1.0 was finalised. So unfortunately OP is correct that this is confusing, but won’t likely be changed any time soon.

---

<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: [February 16, 2020, 1:17pm UTC](https://discourse.julialang.org/t/vector-vs-column-vector/34699/7 "2020-02-16T13:17:50Z")

</div>

> [@dlfivefifty](#):
>
> wasn’t consensus on which to use before Julia v1.0

That, and maybe the fact that while occasionally one does need nx1 matrices, this is not one of the frequent use cases, in contrast to other languages (eg Matlab, AFAIK). I recall needing this maybe twice in 5 years.

---

<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: [February 16, 2020, 5:15pm UTC](https://discourse.julialang.org/t/vector-vs-column-vector/34699/8 "2020-02-16T17:15:27Z")

</div>

I am learning (for a course that require it) numpy, and actually find it more confusing… as there you may have column vectors, row vectors and… adimensional vectors!

In Julia they all are `Array{T,N}`. Then “vectors” are just aliases for column arrays (Array{T,1}), Matrix is an alias for an `Array{T,2}` (and a row vector is a matrix with only one row) and then you can have multidimensional arrays if you with.

As it is expected, they are initiated with a different syntax.

---

<div class="post-metadata">

### Author: ![ea42gh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ea42gh/32/12000_2.png) [@ea42gh](https://discourse.julialang.org/u/ea42gh)
#### Post date: [February 16, 2020, 6:21pm UTC](https://discourse.julialang.org/t/vector-vs-column-vector/34699/9 "2020-02-16T18:21:44Z")

</div>

Here is why I am puzzled:

```julia
[ 1 2
  3 4 ]

```

yields `2×2 Array{Int64,2}` a matrix.

```julia
[ 1
2 ]

```

yields `2-element Array{Int64,1}` a vector

The only way I can imagine this to happen  
is if the implementation goes out of its way to make the column vector (a matrix!) case  
inconsistent.

Is it a failure of imagination on my part?

---

<div class="post-metadata">

### Author: ![ea42gh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ea42gh/32/12000_2.png) [@ea42gh](https://discourse.julialang.org/u/ea42gh)
#### Post date: [February 16, 2020, 7:30pm UTC](https://discourse.julialang.org/t/vector-vs-column-vector/34699/10 "2020-02-16T19:30:49Z")

</div>

I’d quibble with `sensible`:

The definition of `A x = b` either considers x and b to be matrices (column vectors),  
or requires a special definition of matrix times vector.

Silently adding or dropping a constant index might be convenient, but tends to lead to problems. Personally, I prefer explicit conversions in my codes.

One problematic statement are formulae such as  
`\frac{u^t v}{v^t v} v` (dropping two indices) instead of ` \frac{ u \cdot v }{ v \cdot v } v`

---

<div class="post-metadata">

### Author: ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)
#### Post date: [February 16, 2020, 7:38pm UTC](https://discourse.julialang.org/t/vector-vs-column-vector/34699/11 "2020-02-16T19:38:14Z")

</div>

> [@ea42gh](#):
>
> x and b to be matrices (column vectors)

There’s a completely well-defined notion of a vector (a one-dimensional object), and in the modern approach to linear algebra you basically _define_ matrices as transformations operators for vectors in some suitable basis.

> Silently adding or dropping a constant index might be convenient, but tends to lead to problems.

Back when I wrote everything in Matlab, those years were characterized by a series of mild annoyances of always having to worry about whether `a` was a row vector or a column vector, and sprinkling `a(:)` and `a(:)'` like pixie dust to fix problems. It’s a huge relief to have real one-dimensional objects.

If you wonder whether the Julia system is carefully thought out, I think you’ll really enjoy this video: [https://www.youtube.com/watch?v=C2RO34b\_oPM](https://www.youtube.com/watch?v=C2RO34b_oPM)

---

<div class="post-metadata">

### Author: ![ea42gh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ea42gh/32/12000_2.png) [@ea42gh](https://discourse.julialang.org/u/ea42gh)
#### Post date: [February 16, 2020, 7:41pm UTC](https://discourse.julialang.org/t/vector-vs-column-vector/34699/12 "2020-02-16T19:41:52Z")

</div>

Heading there now! 🙂  
And yes, I have become quite aware of the care taken with types and operations  
(deprecations in Julia 0.7 to 1.0 make it obvious!)

---

<div class="post-metadata">

### Author: ![ea42gh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ea42gh/32/12000_2.png) [@ea42gh](https://discourse.julialang.org/u/ea42gh)
#### Post date: [February 16, 2020, 8:47pm UTC](https://discourse.julialang.org/t/vector-vs-column-vector/34699/13 "2020-02-16T20:47:35Z")

</div>

Don’t know: `RowVector` to allow multiple dispatch to automatically apply an isomorphism to do a type conversion seems yet another kludge. Not as bad as MATLAB, perhaps…

And yes, too much pixie dust…

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [February 21, 2020, 4:27pm UTC](https://discourse.julialang.org/t/vector-vs-column-vector/34699/14 "2020-02-21T16:27:20Z")

</div>

A little late, but I think the discussion here is also relevant:

> [@\`eachrow\` over Array{Any, 2} does not return rows, why?](https://discourse.julialang.org/t/eachrow-over-array-any-2-does-not-return-rows-why/34601):
>
> I use Julia often, but mostly JuMP. I have incentivized the use of Julia in my laboratory. Today, one of my laboratory colleagues has said to me he used Julia to make a small program, and while he overall liked the language he found the inconsistency between row and column major to have made him lost some sanity and wasted considerable development time. I was confused because I do not make much use of matrices but this somewhat did not align with I had seen in many discussion about Julia. After …

The best course of action seemed to give a better error message. I will probably work on such enhancement next week. I think it is reasonable that exist a convention that a vector is always assumed to be a column (in the case we need to decide to treat it as a row or as a column) but at the same time there is an specific type to represent a column of an N-dimensional array that is different from the just-a-single-dimension-aware-vector. I just think that, in the context of some matrix operations (`eachrow` in the link above), some methods would make more sense to return a matrix with some unitary dimensions instead following the pattern of _always_ dropping unitary dimensions from methods returns.
