Skip to content
Viral B. Shah edited this page Aug 6, 2011 · 1 revision

Concatenating with comma performs hcat, equivalent to cat(2,...). Concatenating with semicolon performs vcat, equivalent to cat(1,...).

Tensors can be coerced to higher-rank tensors by adding trailing singleton dimensions. For example a 3-vector becomes a 3x1 column matrix. So vectors always behave as columns during concatenation.

A vector can be transposed to produce a 1xN row matrix.

As an exception, for scalar arguments [a, b, c] gives a vector. This is mostly a visual thing and we will see how it goes.

Cell array syntax is different, because it does not do concatenation. {} produces an object whose shape is determined entirely by the commas and semicolons inside, not the shapes of its arguments. For example {x,x,x;x,x,x} is always 2x3. And since 1d cells are more common, {x,x,x} will make a 1d array.

Relationship to comprehensions

Comprehensions could be defined (or explained) in terms of this concatenation operator. The idea is to make comprehensions equivalent to bracket syntax, except the block elements are generated by code instead of written out manually:

[ f(i,j) | i=1:3, j=1:3 ]

would mean:

[ f(1,1), f(1,2), f(1,3);
  f(2,1), f(2,2), f(2,3);
  f(3,1), f(3,2), f(3,3) ] 

So if f returns 3x3 matrices you get a 9x9 matrix.

Comprehensions can still yield N-d arrays based on the indexes:

[ A | i=1,j=1,k=1:3 ]

would yield 3 copies of matrix A stacked in the 3rd dimension.

The [] syntax can only express 2 dimensions because it only has 2 symbols "," and ";". With comprehensions you effectively introduce your own symbols as variable names, making them more flexible. This suggests that [] could express N-d arrays if we had more symbols to use! One could imagine something crazy like

;    dimension 1
,    dimension 2
,,   dimension 3
,,,  dimension 4
,,,, dimension 5
So I could stack my 3 A's in the 3rd dimension using [A,,].