Concatenate vectors or matrices under Matlab
Concatenation of variables Matlab is define as the combination these variables in a single (vector or matrix).
Example:
| 2 3 4 |
X = | 1 2 5 |
| 0 2 7 |
With:
| 9 6 8 |
Y = | 5 6 2 |
| 3 2 1 |
(concatenation of rows):
| 9 6 8 2 3 4 |
Result = | 1 2 5 5 6 2 |
| 0 2 7 3 2 1 |
Or (concatenation of columns):
| 2 3 4 |
| 1 2 5 |
| 0 2 7 |
Result = | 9 6 8 |
| 5 6 2 |
| 3 2 1 |
From here you may have noticed that not all variables can be concatenate.
Horizontal concatenation
Using X and Y (they can be vectors, matrices or simple variable). It is necessary that the two variables to concatenate have the same number of rows. The code is as follows:
Result = [X Y]
Vertical concatenation
Using X and Y (they can be vectors, matrices or simple variable). It is necessary that the two variables to concatenate have the same number of columns. The code is as follows:
Result = [X, Y]
Further calculations
You can concatenate a vector (matrix) with the transposed of the other. For example:
X = | 1 2 |
| 2 |
Y = | 3 |
| 5 |
Using the command:
Result = [X Y]
Result = | 1 2 2 3 5 |
Using the command:
Result = [X ', Y]
| 1 |
| 2 |
Result = | 2 |
| 3 |
| 5 |
The principle remains the same, we can concatenate multiple variables, provided the scale is respected.
For example. Using the following set of variable:
A = 1
B = 2
C = | 3 4 |
| 5 6 |
D = 7
E = | 8 9 |
The command:
Result = [[A;B] C;D E]
| 1 3 4 |
Result = | 2 5 6 |
| 7 8 9 |