Creating Lists in MATLAB - A Comprehensive Guide with Code Samples and Application Examples

In this detailed article, we will explore how to create lists in MATLAB. We’ll cover the fundamentals of list manipulation, its wide range of applications, and offer code samples to demonstrate variou …

Updated November 21, 2023

Today’s sponsor is Depot. Depot is a remote container build service that’s half the cost of GitHub Actions runners and up to 40x faster than other build services. Try it for free.

In this detailed article, we will explore how to create lists in MATLAB. We’ll cover the fundamentals of list manipulation, its wide range of applications, and offer code samples to demonstrate various concepts. By the end, you’ll have a strong understanding of using lists effectively in your MATLAB scripts and programs.


Lists play a crucial role in programming languages as they enable efficient storage and manipulation of data. In this article, we will focus on working with lists within MATLAB and discuss the various ways to create them. We’ll also look at how to perform operations such as sorting, filtering, merging, and more, all while using a multitude of examples to demonstrate concepts clearly.

  1. List Fundamentals in MATLAB In MATLAB, lists are essentially arrays that store data in an ordered manner, typically following the principle of “first-in-first-out” (FIFO). These lists can hold various types of data, such as numbers, strings, and even other arrays, allowing for versatile use cases. To create a list in MATLAB, you simply need to store multiple elements within a single variable using square brackets or commas to separate the values.

  2. Creating Lists with Numeric Values Creating a numeric list is as simple as assigning multiple numbers to a single variable. Here’s an example of creating a list containing integers:

list_int = [1, 3, 5]

To create a list of floats or real-valued data, you can simply use decimals in the list:

list_float = [0.1, 2.6, 4.7]

You may also mix integer and float values within the same list as follows:

mixed_values = [10, 3.5, “Hello”, 4.8]

Notice that in this last example, we have stored not only numeric values but also a string value, highlighting how versatile lists can be in MATLAB.

  1. Sorting Lists in MATLAB Once you have created your list, there may be instances where you need to sort its elements based on their values or properties. MATLAB offers the ‘sort’ function specifically designed for this purpose:

list_int = [1, 3, 5] sorted_list_int = sort(list_int)

The ‘sort’ function will arrange the list in ascending order by default, but you can specify an additional parameter to change this behavior. For instance, if you wanted to sort the integers descendingly, you could do so with:

sorted_desc_list_int = sort(list_int,‘descend’)

  1. Filtering Lists in MATLAB Sometimes, we may need to filter a list based on specific criteria or characteristics of its elements. The ‘find’ function is a useful tool for this purpose, as it allows you to search for elements that meet your requirements and return the corresponding indices:

list_int = [1, 3, 5, 2, 4] even_elements = find(mod(list_int,2)==0)

In this example, we have filtered out even numbers from our list. The ‘mod’ function returns the remainder of a division operation, which can be used to identify whether an element is odd or even by comparing it with 2 (i.e., if the remainder is 0, the number is even).

  1. Merging Lists in MATLAB Merging lists allows you to combine two or more lists into a single, larger list. MATLAB provides several functions for this purpose: ‘cat’, ‘vertcat’, and ‘horzcat’. Each function has its own properties for merging list elements based on their dimensions. Here’s an example using the ‘vertcat’ function to merge two vectors vertically:

list1 = [1, 3, 5] list2 = [2, 4, 6] merged_list = vertcat(list1, list2)

  1. Performing Operations on List Elements in MATLAB In addition to creating and manipulating lists, you can perform operations directly on their elements with ease using MATLAB’s vectorized functions. These functions operate element-wise, meaning they apply the same operation to each element within the list or array. For instance, we could calculate the square root of all elements in a numeric list by employing the ‘sqrt’ function as follows:

list_float = [4, 9, 16] square_roots = sqrt(list_float)

Applications and Examples Lists in MATLAB are not just confined to simple manipulations. They play a crucial role in various applications such as data analysis, signal processing, and image and video processing. Here are some real-world examples where lists prove their worth:

  1. Signal Processing: MATLAB can be used to process audio or video signals by working with sound samples or frame arrays. These data structures involve sequences of numbers representing audio or visual information. By utilizing list manipulation techniques, you can analyze and modify these signals accordingly, enhancing the overall quality or adding new features.

  2. Data Analysis: Lists are instrumental in extracting valuable insights from large datasets. They enable efficient handling of data sets, enabling various analysis tasks such as filtering outliers, performing statistical tests, or even visualizing trends using plots and histograms.

  3. Machine Learning: In the world of machine learning, algorithms commonly operate on tabular data structures containing features and their corresponding labels. This data can be represented as a list of lists, where each inner list consists of an instance’s attributes (features) and the associated label. These data representations are then passed to machine learning algorithms to train a model or make predictions based on the training data.

Conclusion Lists in MATLAB provide powerful means for efficiently storing, manipulating, and analyzing different types of data. In this article, we have explored various ways to create lists, from basic numeric lists to more advanced examples that demonstrate sorting, filtering, merging, and performing operations on elements. By mastering these techniques, you can significantly enhance the efficiency of your MATLAB scripts and programs in multiple application domains.