Matlab

cat Function in MATLAB

MATLAB is a widely used programming language in scientific and engineering fields. One of its valuable features is the cat function, which allows you to combine arrays along a chosen dimension. In this article, we will dive into the world of the cat function in MATLAB and learn how to use it effectively.

What is cat() Function in MATLAB?

The cat is a short form of concatenate is a built-in function in MATLAB that allows you to concatenate two or more single and multidirectional arrays into a single array. By providing the appropriate arguments, you can use the cat function to combine arrays along different dimensions and obtain the desired concatenated array as the output.

Keep in mind that the arrays that you want to concatenate should have the same dimensions.

Syntax for cat() Function in MATLAB?

The cat() function follows a simple syntax that is given below:

c = cat(dim, x, y)
c = cat(dim, x1, x2, x3, ...)

 

Here:

c= cat(dim,x,y) returns an array by concatenating y to the end of the x having dimension dim when x and y have the same dimension.

c = cat(dim,x1,x2,…,xn) concatenates x1, x2, … , xn having dimension dim.

To concatenate arrays, you can utilize the square bracket operator []. For example, concatenating arrays x and y horizontally and vertically use [x,y] and [x y] and [x; y] respectively.

Example 1

This MATLAB code creates two vectors and concatenates them along dimension 1 by using cat() functions.

x= [2: 10];
y= zeros(1, 9);
c= cat(1, x, y)

 

Example 2

The following MATLAB code creates two vectors and concatenates them along dimension 2 by using cat() functions.

x= [2: 10];
y= zeros(1, 9);
c= cat(2, x, y)

 

Example 3

This MATLAB code creates two matrices and concatenates them along dimension 1 by using cat() functions.

x= [2:4; 3:2:7; zeros(1, 3)];
y= ones(3);
c= cat(1, x, y)

 

Example 4

This MATLAB code creates two matrices and concatenates them along dimension 2 by using cat() functions.

x= [2:4; 3:2:7; zeros(1, 3)];
y= ones(3);
c= cat(2, x, y)

 

Example 5

In this MATLAB code, we concatenate the two matrices horizontally in a cell array to create a single matrix.

x= [2:4; 3:2:7; zeros(1, 3)];
y= ones(3);
x1= {x, y};
c = cat(2,x1{:})

 

Conclusion

The cat() function in MATLAB is a powerful tool that enables you to concatenate single and multidimensional arrays, merging them into a single array. By providing the necessary parameters, you can utilize this function to combine arrays effectively. In this tutorial, we have discussed the working of the cat() function in MATLAB, providing examples to illustrate its usage.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.