# What's the meaning of the array syntax , ;

**URL:** https://discourse.julialang.org/t/whats-the-meaning-of-the-array-syntax/938
**Category:** New to Julia
**Created:** [December 14, 2016, 11:25am UTC](https://discourse.julialang.org/t/whats-the-meaning-of-the-array-syntax/938 "2016-12-14T11:25:02Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![emil](https://avatars.discourse-cdn.com/v4/letter/e/f4b2a3/32.png) [@emil](https://discourse.julialang.org/u/emil)
#### Post date: [December 14, 2016, 11:25am UTC](https://discourse.julialang.org/t/whats-the-meaning-of-the-array-syntax/938/1 "2016-12-14T11:25:02Z")

</div>

Hello,

have been playing with Julia for a few weeks. (I have used MATLAB, but more of a Python background.) I find the following almost impossible to remember when typing arrays manually:

- spaces yield horizontal arrays
- commas yield vertical arrays
- semicolons yield vertical arrays
- semicolons and spaces yield 2x2 arrays
- commas and spaces yield errors
- commas with subarrays yield arrays-of-arrays
- semicolons with subarrays yield flat arrays

c.f. [https://github.com/JuliaLang/julia/blob/master/doc/src/manual/arrays.md](https://github.com/JuliaLang/julia/blob/master/doc/src/manual/arrays.md)

It’s hard to remember because I can’t see a philosophy. Commas and semicolons seem to just behave differently in ways that are not predictable (from remembering just 6 out of the above 7 facts, I can never deduce the 7th fact). Maybe semicolons are distinguished from commas by concatenating “more aggressively”, but if this is the only difference, why can’t matrices be declared as `[1 2, 3 4]`?

It creates confusion for me later in the documentation, because for later statements (e.g. “An array with a specific element type can be constructed using the syntax `T[A, B, C, ...]`.”) I am not sure if it is supposed to be general, or refers to a particular property of `,` versus `;`. (Of course, in this case it doesn’t, but as a beginner I have to test each case to check.)

What’s the logic distinguishing `,` from `;` for array creation?

---

<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: [December 14, 2016, 11:28am UTC](https://discourse.julialang.org/t/whats-the-meaning-of-the-array-syntax/938/2 "2016-12-14T11:28:07Z")

</div>

Think of commas as syntax for enumeration, and spaces and semicolons as concatenation operators.

```julia
julia> z = zeros(2,2)
2×2 Array{Float64,2}:
 0.0 0.0
 0.0 0.0

julia> o = ones(2,2)
2×2 Array{Float64,2}:
 1.0 1.0
 1.0 1.0

julia> [z o; o z]
4×4 Array{Float64,2}:
 0.0 0.0 1.0 1.0
 0.0 0.0 1.0 1.0
 1.0 1.0 0.0 0.0
 1.0 1.0 0.0 0.0

```

---

<div class="post-metadata">

### Author: ![emil](https://avatars.discourse-cdn.com/v4/letter/e/f4b2a3/32.png) [@emil](https://discourse.julialang.org/u/emil)
#### Post date: [December 14, 2016, 11:48am UTC](https://discourse.julialang.org/t/whats-the-meaning-of-the-array-syntax/938/3 "2016-12-14T11:48:02Z")

</div>

That describes some of the observed behaviour, thank you. But I am still wondering two things:

Firstly, why isn’t `[z o, o z]` given any meaning in your example? For example, it could give a 2x2 array of 2x2 arrays.

Secondly, what completes this table? We have comma, semicolon, and space, but no ‘horizontal enumerator’.

```
     | enum. | conc. |
----------------------
vert | , | ; |
horz | ??? | [_] |

```

---

<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: [December 14, 2016, 12:02pm UTC](https://discourse.julialang.org/t/whats-the-meaning-of-the-array-syntax/938/4 "2016-12-14T12:02:38Z")

</div>

Possibly because enumeration is directionless: it gives flat structures, isomorphic to vectors. `,` is not a _vertical_ separator, just a separator. Convention considers vectors “vertical”, but they are just vectors, directionless _per se_. In light of this, I don’t think your table is the right mental model.

`[z o, o z]` could be given meaning, but space/`;` is special syntax for a matrix expression and you can’t mix it with anything else (which IMO is a solid design choice). Try `[[z o], [o z]]`.

---

<div class="post-metadata">

### Author: ![emil](https://avatars.discourse-cdn.com/v4/letter/e/f4b2a3/32.png) [@emil](https://discourse.julialang.org/u/emil)
#### Post date: [December 14, 2016, 1:23pm UTC](https://discourse.julialang.org/t/whats-the-meaning-of-the-array-syntax/938/5 "2016-12-14T13:23:45Z")

</div>

That’s an interesting perspective and does help fill in the gap for me. But it makes me think that, if comma gives a vector and space gives a `1xn Array{T,2}`, then `;` would produce an `nx1 Array{T,2}`, so I still cannot completely predict the rules.

Regardless, it might be nice to spell out the meaning behind this design choice in the documentation more explicitly, or at least to clarify that `,;` are interchangeable for vectors but only `;` applies to matrices.

And I still feel that `[z o, o z]` could be a 2x2 array of 2x2 arrays. It has a certain logic, I see no drawback, and `[[z o], [o z]]` is very different in input and in output.

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [December 14, 2016, 1:30pm UTC](https://discourse.julialang.org/t/whats-the-meaning-of-the-array-syntax/938/6 "2016-12-14T13:30:52Z")

</div>

No, `;` and `,` are not interchangeable for vectors:

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

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

```

i.e. `;` concatenates.

---

<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: [December 14, 2016, 1:34pm UTC](https://discourse.julialang.org/t/whats-the-meaning-of-the-array-syntax/938/7 "2016-12-14T13:34:25Z")

</div>

> [@emil](#):
>
> spell out the meaning behind this design choice

The interface is still in flux. See in particular

> <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.

> <https://github.com/JuliaLang/julia/issues/10338>
>
> Some days (by now weeks) ago, I started working on cleaning up and improving the… efficiency of the concatenation code (\`hcat\`, \`vcat\`, \`cat\`, ... ? ). A first attempt was #10155, but I closed it in favor a fresh restart. However, in the mean time several questions and comments have arisen (see e.g. https://groups.google.com/forum/#!topic/julia-users/E3G686bg9lE, #10204, ... ). So I thought it was good to have a poll about a number of decisions.
> \- I think most people agree on #8599 , which will become in effect after the depreciation period, that \`\[a, b, c\]\` should just construct a vector of its elements and not concatenate. \`T\[a, b, c\]\` can be used to specify the element type of the vector and \`convert\` all elements \`a,b,c\` to type \`T\`.
> \- \`\[a; b; c\]\`, \`\[a b c\]\` and \`\[a b; c d\]\` currently concatenate, i.e. they expand arguments \`a\`, \`b\`, .. of type \`AbstractArray\` into their individual elements. There also is a typed version \`T\[a; b; c\]\` etc to force the element type of the resulting output array. There are the following critiques (not necessarily my personal opinion):
> 1. The syntax \`\[a b; c d\]\` is also useful to construct a \`Matrix\` by just specifying simple arguments (e.g. \`a,b,c,d\` of type \`Number\`). This is not really concatenation, just filling a matrix. Also, there is no way to construct a \`(n,1)\` or \`(1,1)\` Matrix. See also https://groups.google.com/forum/m/?fromgroups#!topic/julia-users/E4kX3XGzao4 .
> 2. It is impossible to build \`Matrix\` with elements of type \`AbstractArray\` using this syntax, since they will always concatenate. In fact, trying to use the typed syntax such as \`Vector{Int}\[a b c\]\` just fails (#10204).
> 3. This lead to the proposal to get rid of the typed concatenation syntax all together (#10204) and disallow the construction of arrays of arrays using the concatenation interface. 
> 4. Other questions: how should the return array type of concatenation depend on the input, currently most cases use \`Array\`, but some cases return e.g. \`BitArray\` or some sparse matrix, but this is hard to alter this behavior in the case of mixed inputs (e.g. https://github.com/JuliaStats/DataArrays.jl/issues/130), especially if you still want to maintain high efficiency for the simpler case and don't want to write many different method definitions of the same functions.
> 
> I could see two different proposals two continue
> 
> 1) Basically the current proposal:
> \- \`\[a,b,c\]\` never concatenates, it can only be used to construct \`Vector\` objects.
> \- \`\[a b c\]\` and \`\[a; b; c\]\` always concatenate, you could not use this syntax to build arrays of arrays, there would still need to be a decision regarding the fate of the typed concatenation syntax and question iv.
> 
> 2) A new proposal:
> \- \`\[a, b, c\]\`, \`\[a b; c d\]\` etc never concatenate. \`\[a, b, c\]\` is a constructor for type \`Vector\` and thus creates a vector its elements (size \`(3,)\`). \`\[a b c\]\`, \`\[a; b; c\]\` and \`\[a b; c d\]\` are \`Matrix\` constructors and thus create matrices of respectively sizes \`(1,3)\`, \`(3,1)\`, \`(2,2)\`. In particular, \`\[a;\]\` can be used to create a \`Matrix\` of size \`(1,1)\`. They all can be combined with a type \`T\` specification and it is possible to have elements which are themselves arrays.
> \- A new syntax (e.g. using \`\[| ... |\]\` brackets) is used for concatenation. There might still be a use case to distinguish between \`,\` and \`;\` although I don't immediately see one.

and for some history

> <https://github.com/JuliaLang/julia/issues/3737>
>
> This works:
> 
> \`\`\`
> julia\> \[1:3, 4 \]
> 4-element Int64 Array:
> 1
> 2
> 3
> 4
> \`\`\`
> 
> and t…his works:
> 
> \`\`\`
> julia\> Uint8\[1:3 \]
> 3-element Uint8 Array:
> 0x01
> 0x02
> 0x03
> \`\`\`
> 
> but this doesn't:
> 
> \`\`\`
> julia\> Uint8\[1:3, 4\]
> ERROR: no method convert(Type{Uint8},Range1{Int64})
> in setindex! at array.jl:392
> in getindex at array.jl:161
> \`\`\`
> 
> Is this expected behavior? I would expect to be able to construct Uint8 arrays this way.

> <https://github.com/JuliaLang/julia/pull/8599>
>
> Brings @nolta's \`mn/sanecat\`branch up to date. Addresses #3737 #2488. Part of… #7941.

and related

> <https://github.com/JuliaLang/julia/issues/7128>
>
> Much gnashing of teeth derives from the overlap between syntax for array literal… construction and array concatenation in Julia – largely inherited from Matlab. Perhaps we should just use a different syntax for block matrix construction entirely. One thought would be this:
> 
> \`\`\` julia
> | a b
> c d |
> \`\`\`
> 
> This has the advantage of being pretty terse and lightweight. For example, the current idiom of expanding a range into an array is \`\[1:10\]\` which would become \`|1:10|\` while \`\[1:10\]\` would construct a one-element array of type \`UnitRange{Int}\`.

If you think you can contribute to better design, you should probably comment on Github.

---

<div class="post-metadata">

### Author: ![emil](https://avatars.discourse-cdn.com/v4/letter/e/f4b2a3/32.png) [@emil](https://discourse.julialang.org/u/emil)
#### Post date: [December 14, 2016, 5:08pm UTC](https://discourse.julialang.org/t/whats-the-meaning-of-the-array-syntax/938/8 "2016-12-14T17:08:22Z")

</div>

It looks like for many people, the preferred solution is to disentangle array construction and concatenation ops. I agree with that. That idea has not gained traction in some years, although it should happen ASAP before 0.6/1.0 if it will ever happen …

A smaller improvement to the current situation along the above lines could be to make `;` return a `nx1 Array`. That would make more sense to me, but it then seems to suggest changing `vcat` to return an `nx1 Array`. Thoughts?

---

<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: [December 14, 2016, 7:37pm UTC](https://discourse.julialang.org/t/whats-the-meaning-of-the-array-syntax/938/9 "2016-12-14T19:37:39Z")

</div>

This has changed significantly in 0.4 and 0.5 (and possibly already in 0.3, I forget). That’s as fast as it could be changed without silently breaking people’s code. Changing syntax in a system that’s actively used by a lot of people is slow, delicate work. Using `[]` with `,` separators now only ever means array construction whereas `;` and space as separators only mean vertical and horizontal concatenation, respectively (which is why you can’t mix `,` with `;` or space). So this is now consistent once you understand what they mean,and it lets you do both kinds of operations.

I used to feel that we should get rid of space-sensitive syntax everywhere, but that was partly because there were occasional little bugs with it. It’s been a long time since there have been any such bugs and people seem to have gotten used to it – especially since the rules are now consistent everywhere there’s a space-sensitive context.

Are you suggesting that we stop using `[]` for array concatenation entirely? Or are there some corner cases of `[]` syntax that you’ve found annoying? Specifically, do you mean that you think `[1;2;3]` should produce a 3x1 matrix rather than a 3-element vector? I suppose that there’s a certain consistency to having all concatenation operations produce matrices, but that would leave us without the ability to write `[v;w]` to concatenate two vectors and get another vector, so there’s a tradeoff. If you’ve got a good argument for why it’s a better choice, I’d love to hear it.

---

<div class="post-metadata">

### Author: ![jballanc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jballanc/32/328_2.png) [@jballanc](https://discourse.julialang.org/u/jballanc)
#### Post date: [December 15, 2016, 7:33am UTC](https://discourse.julialang.org/t/whats-the-meaning-of-the-array-syntax/938/10 "2016-12-15T07:33:46Z")

</div>

What about embracing space-sensitive syntax fully?

Personally, what I find confusing is trying to remember that `;` means vertical _concatenation_. One source of confusion is that `[1,2,3]` and `[1;2;3]` generate the same result, but for different reasons. Luckily, a newline works as a stand-in for `;` and does pretty much exactly what you’d expect:

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

julia> b = [3 4]
1×2 Array{Int64,2}:
 3 4

julia> [a b
        b a]
2×4 Array{Int64,2}:
 1 2 3 4
 3 4 1 2

```

In fact, one can avoid the whole confusion around `,` and `;` entirely and just use whitespace instead (and if you are worried about taking up too many lines of code but still want to avoid the confusion, there’s always `hcat` and `vcat`).

---

<div class="post-metadata">

### Author: ![emil](https://avatars.discourse-cdn.com/v4/letter/e/f4b2a3/32.png) [@emil](https://discourse.julialang.org/u/emil)
#### Post date: [December 15, 2016, 8:02am UTC](https://discourse.julialang.org/t/whats-the-meaning-of-the-array-syntax/938/11 "2016-12-15T08:02:37Z")

</div>

No, I don’t want to suggest any really big changes, not now.

It would make more sense to me if `;` produced an `nx1` matrix instead of a vector. So the simple rule becomes: _commas do vectors, semicolons and spaces concatenate and produce matrices._

That would entail the following changes.

- `vcat` produces `nx1` matrices.
- `hcat` produces `1xn` matrices
- `hvcat` produces `mxn` matrices
- a new function `cat` takes vectors and returns a vector

The idea is that `hcat` and `vcat`, with their direction explicitly included, only make sense in a 2D context, and it helps separate vectors and `1xn/nx1` matrices more meaningfully. The convenient `[v;w]` becomes `cat(v,w)` or `cat([v,w])`.

To me that’s cleaner, but maybe other beginners would be confused by distinguishing vectors/`1xn`,`nx1` matrices this way. Would be interested to get other people’s ideas.

---

<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: [December 15, 2016, 9:05am UTC](https://discourse.julialang.org/t/whats-the-meaning-of-the-array-syntax/938/12 "2016-12-15T09:05:12Z")

</div>

> [@emil](#):
>
> It would make more sense to me if ; produced an nx1 matrix instead of a vector.

I can sympathize with this.

---

<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: [December 15, 2016, 2:11pm UTC](https://discourse.julialang.org/t/whats-the-meaning-of-the-array-syntax/938/13 "2016-12-15T14:11:40Z")

</div>

> - vcat produces nx1 matrices.
> - hcat produces 1xn matrices

Minor point: `vcat` and `hcat` can produce arbitrary width and height matrices if their inputs are wide/tall respectively. E.g.:

```julia
julia> [rand(2,2); rand(3,2)]
5×2 Array{Float64,2}:
 0.187175 0.629967
 0.706591 0.938334
 0.781615 0.0100944
 0.430575 0.95716
 0.0332201 0.211566

julia> [rand(2,2) rand(2,3)]
2×5 Array{Float64,2}:
 0.00450999 0.636033 0.674548 0.369836 0.925724
 0.88356 0.163572 0.472557 0.960674 0.407421

```

---

<div class="post-metadata">

### Author: ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)
#### Post date: [December 15, 2016, 3:28pm UTC](https://discourse.julialang.org/t/whats-the-meaning-of-the-array-syntax/938/14 "2016-12-15T15:28:35Z")

</div>

> [@emil](#):
>
> It would make more sense to me if ; produced an nx1 matrix instead of a vector. So the simple rule becomes: commas do vectors, semicolons and spaces concatenate and produce matrices.

I agree too that this you make more sense. Right now, it seems [the recommended way](http://cheatsheets.quantecon.org/) to make an nx1 array is with the transpose operation, which could lead to unnoticed mistakes if you don’t realize that it gives a complex transpose by default:

```julia
julia> [1+2im 2+3im]'
2×1 Array{Complex{Int64},2}:
 1-2im
 2-3im

```

I was actually surprised at the current behaviour since [the manual (very vaguely) implies](http://docs.julialang.org/en/release-0.5/manual/arrays/#concatenation) that the semicolon gives “arrays” as opposed to the line right above that explain the comma gives “1-d arrays”. The special syntax table also gives the impression spaces and semicolons should output the same type of object.

---

<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: [December 18, 2016, 9:30am UTC](https://discourse.julialang.org/t/whats-the-meaning-of-the-array-syntax/938/15 "2016-12-18T09:30:42Z")

</div>

Additionally you can create nx1 array from 1-d vector (or iterable) is by using `hcat`, eg.

```julia
hcat([1, 2, 3])
hcat(1:3)

```

---

<div class="post-metadata">

### Author: ![pitao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pitao/32/116_2.png) [@pitao](https://discourse.julialang.org/u/pitao)
#### Post date: [December 18, 2016, 11:17am UTC](https://discourse.julialang.org/t/whats-the-meaning-of-the-array-syntax/938/16 "2016-12-18T11:17:07Z")

</div>

That cheat sheet is incorrect.

```
A.' # transpose of A
A' # CONJUGATE transpose of A

```

(i.e. Same as Matlab)

PS there is a whole separate discussion about the merits of having a syntax use `.` in a non-broadcasty way.

---

<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: [January 3, 2017, 9:08pm UTC](https://discourse.julialang.org/t/whats-the-meaning-of-the-array-syntax/938/17 "2017-01-03T21:08:11Z")

</div>

x-ref: [https://github.com/JuliaLang/julia/issues/19622](https://github.com/JuliaLang/julia/issues/19622)

---

<div class="post-metadata">

### Author: ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)
#### Post date: [January 3, 2017, 11:29pm UTC](https://discourse.julialang.org/t/whats-the-meaning-of-the-array-syntax/938/18 "2017-01-03T23:29:45Z")

</div>

I have to say that I am not a fan of `vcat` returning a matrix even if the concatenated arrays are one-dimensional. I do use vectors more than matrices, and overall I think a one-dimensional vector is a more useful result type than an n×1 matrix in a greater number of situations.
