MATLAB cat function syntax
c = cat (a1, a2)
c = cat (dim, a1, a2…an)))
MATLAB cat function description and examples
The cat() function concatenates two or more arrays and can do so with 2D or multidimensional arrays. cat() returns in “c” the concatenation of the arrays a1, a2…an. In each case, these arrays must be the same size or compatible in the dimensions to be concatenated; otherwise, an error message is returned. The input arguments accepted by this function are scalar, vector, matrix, multidimensional-array, table and time. Next, we will show you the types of input arguments, control flags and data types accepted by each argument.
dim: Specifies along which dimension the concatenation will be performed. Positive integer scalar
a1, a2 and an: input arrays. Scalar, vector, matrix, multidimensional-array, table and timetable.
How to concatenate two vectors with the MATLAB cat() function
The following example shows how you can concatenate two vectors using the MATLAB function cat(). The result is the creation of the vector “c”, which contains the vectors “v1” and “v2” concatenated.
v2 = [3, 4];
c = cat(2, v1, v2)
c = 1 2 3 4
In this case, the concatenation is horizontal, so the cat() function returns a row vector in which the vectors “v1” and “v2” are concatenated.
Now we will see how to concatenate the vectors “v1” and “v2” vertically with the MATLAB cat() function.
v2 = [3, 4];
c = cat(1, v1, v2)
c =
1 2
3 4
In this case, cat() returns in “c” an array of 2 x4 elements with the vertical concatenation of “v1” and “v2”.
How to vertically concatenate two arrays with the MATLAB cat() function
The following example shows how to vertically concatenate two arrays using the MATLAB function cat(). For this purpose, we create arrays “a1” and “a2” with 3×2 elements and concatenate them in “c”. The arrays “a1” and “a2” are sent as an input argument to the cat() function, and we specify via the input “dim” that the concatenation should be performed in dimension 1.
a2 = [5, 6; 7, 8];
c = cat (1, a1, a2)
The cat() function returns an array in “c” containing the vertical concatenation of “a1” and “a2”. Since the concatenation was done vertically, the returned matrix consists of 4 x 3 elements.
c =
1 2
3 4
5 6
7 8
How to horizontally concatenate two arrays with the MATLAB cat() function
Now let’s see how we can concatenate two arrays horizontally. We pass the arrays “a1” and “a2” created in the previous example as input arguments. Through “dim”, we tell the cat() function to concatenate horizontally or in dimension 2.
a2 = [5, 6 ; 7, 8];
c = cat (2, a1, a2)
The cat() function returns an array with the horizontal concatenation of “a1” and “a2”. Since the concatenation was horizontal, cat() in “c” returns an array with 2 x6 elements (see below):
c =
1 2 5 6
3 4 7 8
How to concatenate more than two arrays with the MATLAB cat() function
In this example, we will see how to concatenate three arrays together. To do this, we will create the arrays “a1”, “a2”, and “a3”. This process is done in the same way as in the previous examples. We send the arrays to be concatenated, each separated by commas, and specify in “dim” the dimension in which the concatenation is performed.
a2 = [3, 4; 9, 10];
a3 = [5, 6; 11, 12];
c = cat (2, a1, a2, a3)
In this way, cat() in “c” returns a 2 x 6 matrix in which the three arrays are horizontally concatenated. The order of concatenation corresponds to the input order of the arrays, i.e. “s1” is concatenated with “s2” and this with “s3”.
c =
1 2 3 4 5 6
7 8 9 10 11 12
In the same way, it is done to concatenate more than two arrays vertically; only the dimension must be indicated in the “dim” input.
a2 = [3, 4; 9, 10];
a3 = [5, 6; 11, 12];
c = cat (1, a1, a2, a3)
c =
1 2
7 8
3 4
9 10
5 6
11 12
How to concatenate two or more arrays with brackets in MATLAB
The cat() function is recommended for concatenating and creating arrays with more than two dimensions. In cases where 2D concatenation is required, brackets are recommended, as shown below.
a2 = [3, 4; 9, 10];
c=[a1,a2]
c =
1 2 3 4
7 8 9 10
In this case, the brackets perform the concatenation horizontally, resulting in a matrix of 2 x 4 elements in “c”.
Now we will see how to concatenate two matrices with brackets. We need to separate the arrays to be concatenated with a semicolon, as shown below.
a2 = [3, 4; 9, 10];
c = [a1; a2]
c =
1 2
7 8
3 4
9 10
In this case, the concatenation is vertical, so an array of 4 x2 elements is created in “c”, concatenating the arrays “a1” and “a2”.
Conclusion:
This article has explained how to concatenate arrays of different dimensions in MATLAB using the cat() function. Also, some practical examples have been given to explain how to use this function. Supported argument types and supported data types have also been described in detail. We hope that you found this MATLAB article useful. See other Linux Hint articles for more tips and information.