Language Reference


Concatenation Operator, Horizontal:   ||

  • matrix1 $\parallel $ matrix2;

The horizontal concatenation operator (||) produces a new matrix by horizontally joining matrix1 and matrix2. The matrices must have the same number of rows, which is also the number of rows in the new matrix. The number of columns in the new matrix is the number of columns in matrix1 plus the number of columns in matrix2.

For example, the following statements produce the matrix c, shown in Figure 24.4:

a = {1 1 1,
     7 7 7};
b = {0 0 0,
     8 8 8};
c = a||b;
print c;

Figure 24.4: Result of Horizontal Concatenation

c
1 1 1 0 0 0
7 7 7 8 8 8



For character operands, the element size in the result matrix is the larger of the two operands. For example, the following statements produce a matrix f which has elements of size 2, which are shown in Figure 24.5:

d = {A B C,
     D E F};
e = {"GH" "IJ",
     "KL" "MN"};
f = d||e;
print f;

Figure 24.5: Result of Horizontal Concatenation of Character Matrices

f
A B C GH IJ
D E F KL MN



You can use the horizontal concatenation operator when one of the arguments has no value. For example, if X has not been defined and Y is a matrix, X||Y results in a new matrix equal to Y, as shown in the following statements:

x = {};      /* define empty matrix */
y = 1:3;
z = x || y;
print z;

Figure 24.6: Concatenation of an Empty Matrix

z
1 2 3