# Appending Elements to a List in MATLAB - A Comprehensive Guide with Code Samples

In this extensive article, we will delve deep into the world of Matlab by focusing on one particular aspect – appending elements to a list. We will explore multiple methods such as append, horizontal …

Updated November 18, 2023

*Hey! If you love Computer Vision and AI, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!*In this extensive article, we will delve deep into the world of Matlab by focusing on one particular aspect – appending elements to a list. We will explore multiple methods such as append, horizontal concatenation, vertical concatenation, and more, to understand how these operations are conducted in MATLAB. By the end, you will have a clear picture about how to efficiently utilize these functions in your own Matlab scripts while using them effectively for various applications including computer vision tasks. We’ll also render this article in valid Markdown format with code samples throughout the tutorial to help demonstrate the concepts better.

## Introduction

MATLAB is a high-level programming language specifically designed for technical computing and data visualization. Its built-in functions and powerful features make it an indispensable tool across numerous disciplines, including engineering, computer vision, deep learning, finance, and more. One of the fundamental operations in MATLAB involves manipulating and managing lists or arrays.

In this article, we will focus on a particular aspect of such list handling – appending elements to an existing list. We’ll explore various methods to achieve this task including using built-in functions such as `append`

, horizontal concatenation, vertical concatenation, and more. By the end of this tutorial, you will have gained a better understanding of these operations along with their usage for different scenarios.

## Appending Elements: The append Function

The `append`

function in MATLAB is specifically designed to add elements at the end of an existing list or array while preserving its original properties. It can be used to extend a 1D vector, 2D matrix, or multidimensional array by concatenating additional arrays or rows and columns.

### Syntax:

`append(A,B)`

In the above syntax, `A`

represents the initial array and `B`

represents the data that needs to be appended. Both `A`

and `B`

can contain any combination of vector, matrix, or multidimensional arrays. Here are a few examples illustrating how we can use the `append`

function to append different types of arrays:

- Adding two vectors together horizontally (concatenate rows):

```
A = [2 4 6; 8 9 10];
B = [10 13 16; 19 22 25];
C = append(A, B);
C =
2 4 6 8 9 10 10 13 16 19 22 25
```

- Extending a vector vertically by appending another array:

```
A = [1 2];
B = [3 4; 5 6];
C = append(A, B);
C =
1 2
3 4
5 6
```

- Combining two matrices by adding rows and columns:

```
A = [1 2; 3 4; 5 6];
B = [7 8; 9 10; 11 12];
C = append(A, B);
C =
1 2 7 8
3 4 9 10
5 6 11 12
```

## Horizontal Concatenation: The [ ] Operator

The square bracket notation `[]`

in MATLAB allows you to perform horizontal concatenation by merging two or more arrays together. This is essentially the same as appending elements horizontally, similar to the `append`

function’s behavior. To use this method, simply place multiple arrays within the brackets separated by spaces, commas, or semicolons:

```
A = [1 2; 3 4];
B = [5 6; 7 8; 9 10];
C = [A B];
C =
1 2 5 6
3 4 7 8
9 10 5 6
```

In this example, we have concatenated two separate arrays `A`

and `B`

horizontally. The resulting array `C`

contains three rows (rows of `A`

, followed by rows of `B`

) with the combined column values from `A`

and `B`

.

## Vertical Concatenation: The cat Function

The `cat`

function in MATLAB allows for vertical concatenation, which essentially stacks arrays on top of one another. You can use this method to combine multiple arrays vertically while maintaining their column properties. To perform vertical concatenation, we pass the arrays as arguments to the `cat`

function and specify the dimension along which concatenation should take place using the ‘dim’ parameter:

```
A = [1 2; 3 4];
B = [5 6; 7 8; 9 10];
C = cat(2, A, B);
C =
1 2 5 6 7 8
3 4 9 10 5 6
```

In this example, we’ve used `cat`

to concatenate arrays `A`

and `B`

vertically along the second dimension (dimension 2). The resulting array `C`

has six columns (three from `A`

, followed by three from `B`

) and two rows containing all values from both input arrays.

## Conclusion

In this comprehensive guide, we have explored various methods of appending elements to a list in MATLAB. We started with the `append`

function, which is designed specifically for adding elements at the end of an array while preserving its original properties. Additionally, we discussed using square brackets [ ] for horizontal concatenation and the `cat`

function for vertical concatenation along different dimensions. These functions are powerful tools in MATLAB that can greatly simplify your programming tasks related to list manipulations. With a deep understanding of these techniques, you will be well equipped to tackle more complex problems involving array operations and data processing within the realm of computer vision and beyond.