MATLAB - Cell Arrays



In MATLAB, a cell array is a flexible data structure that allows you to store data of different types and sizes. This is different from regular arrays where the elements in the array have to be of the same data type.

The cell arrays provide flexibility and are particularly useful when dealing with mixed data.The cell arrays are very good in dealing with complex datasets and enabling the creation of more dynamic and efficient MATLAB code.

Cell Array V/S Normal Array

Cell Array and Normal Array are both data structures in Matlab. Here is the difference between them −

Cell Array Normal Array
A cell array can store elements of different data types, such as numbers, strings, other arrays, or even other cell arrays. A normal array (numeric array or matrix) can only store elements of the same data type. For example, if you create a numeric array, all elements must be numbers of the same data type (e.g., double, int, etc.).
Cell arrays use curly braces {} for indexing and accessing individual elements. Normal arrays use parentheses () for indexing and accessing elements.
Cell arrays can dynamically grow or reduce during runtime, allowing you to add or remove elements as needed without pre-allocating any memory. Normal arrays have a fixed size once they are created. To change the size of a normal array, you need to create a new array and copy the data.
Cell arrays are memory efficient when dealing with varying sizes of data elements. Normal arrays have a fixed size, and if you need to store data you will have to allocate extra memory.
Accessing elements in a cell array may be slightly slower than accessing elements in a normal array and that is because of the complex data structure of handling diff data type and size. Accessing elements in a normal array is generally faster as all elements are of the same data type.
Cell arrays are suitable for handling complex and mixed data Cell arrays are suitable for handling complex and mixed data.They are good in handling multiple output functions and organizing data hierarchically. Normal arrays are ideal for numerical computations, matrix operations, and applications where data is of uniform type and fixed size.

Description and Creation of Cell Arrays

You can create cell array by −

  • Using cell array operator {}
  • Using cell() function

To create cell array by using operator {}

A cell array is a data structure comprising indexed data containers known as cells, wherein each cell has the flexibility to store data of any type.

To create a cell array we need to make use of braces {}.

Example

C = {'2023-07-23',[10 20 30 40]}

On executing in matlab you will get the following −

>> C = {'2023-07-23',[10 20 30 40]}

C =
{
  [1,1] = 2023-07-23
  [1,2] =

     10   20   30   40

}

>> 

The cell array we created above is of size 1x2 i.e one row and two columns.

Let us add another row to it as shown below −

C(2,:) = {'2023-07-24',[11 22 33 44]};

On execution will get below cell −

C =

  2x2 cell array

   {'2023-07-23'}    {[10 20 30 40]}
   {'2023-07-24'}    {[11 22 33 44]}

>> 

Let us add one more row again.

C(3,:) = {'2023-07-25',[1 2 3 4]};

On execution the output is −

C =

  3x2 cell array

   {'2023-07-23'}    {[10 20 30 40]}
   {'2023-07-24'}    {[11 22 33 44]}
   {'2023-07-25'}    {[    1 2 3 4]}

>> 

So we now have 3 rows and 2 columns making it a 3x2 cell array.

Now to access the first row you can simply do as follows −

C(1,:)

The output on execution is −

>> C(1,:)

ans =

  1x2 cell array

   {'2023-07-23'}    {[10 20 30 40]}

>> 

To access the 2nd columns from 1st row you can simply do it as follows −

C(1,2)

On execution the output is −

>> C(1,2)

ans =

  1x1 cell array

   {[10 20 30 40]}

>>

You can also create empty cell as shown below −

A = {}

On execution you should see as shown below −

>> A = {}

A =

   0x0 empty cell array

>> 

Once you have an empty cell Array you can add data to it as you wish.

Using cell() Function

This method allows you to preallocate a cell array so that you can assign data later.

Syntax

C = cell(n)
C = cell(sz1,...,szN)
C = cell(sz)
  • Creating a cell array with size nxn cell(n) returns an n-by-n cell array with empty matrices.
  • Creating a cell array with size sz1-by-...-by-szN cell(sz1,...,szN) returns you an sz1,...,szN cell array with empty matrices. Here sz1..szN indicates the size of each dimension. For example cell(3,3) will return a cell array of size 3x3.
  • Creates an empty cell array of size sz vector. For example cell([3,3]) will return you a 3x3 cell array.

Let us see a few examples using cell() function.

Example 1

A = cell(2)

On execution in matlab command window the output is −

>> A = cell(2)

A =
{
  [1,1] = [](0x0)
  [2,1] = [](0x0)
  [1,2] = [](0x0)
  [2,2] = [](0x0)
}

>> 

Example 2

In this example will create a cell array of size 3x3x3. The cell array will be empty on creation but its size will be 3x3x3.

C = cell(3,3,3);

On execution you get following output −

>> C = cell(3,3,3)

  3x3x3 cell array

C(:,:,1) = 

   {0x0 double}    {0x0 double}    {0x0 double}
   {0x0 double}    {0x0 double}    {0x0 double}
   {0x0 double}    {0x0 double}    {0x0 double}

C(:,:,2) = 

   {0x0 double}    {0x0 double}    {0x0 double}
   {0x0 double}    {0x0 double}    {0x0 double}
   {0x0 double}    {0x0 double}    {0x0 double}

C(:,:,3) = 

   {0x0 double}    {0x0 double}    {0x0 double}
   {0x0 double}    {0x0 double}    {0x0 double}
   {0x0 double}    {0x0 double}    {0x0 double}

>> 

Example 3

In this example will create an empty cell array and later update data in it. The data will be of different types.

C = cell(2, 3);

On execution you will see empty cell array of size 2x3 as shown below −

>> C = cell(2, 3)

C =

  2x3 cell array

   {0x0 double}    {0x0 double}    {0x0 double}
   {0x0 double}    {0x0 double}    {0x0 double}

>>

Now let us assign data to each of the cells.

C{1, 1} = 'Hello';
C{1, 2} = 50;
C{1, 3} = [1, 2, 3];
C{2, 1} = 3.14;
C{2, 2} = 'TEST';
C{2, 3} = magic(3);

On execution will get the following −

>> C = cell(2, 3)

C =

  2x3 cell array

   {0x0 double}    {0x0 double}    {0x0 double}
   {0x0 double}    {0x0 double}    {0x0 double}

>> C{1, 1} = 'Hello';
C{1, 2} = 50;
C{1, 3} = [1, 2, 3];
C{2, 1} = 3.14;
C{2, 2} = 'TEST';
C{2, 3} = magic(3);

>> C

C =

  2x3 cell array

   {'Hello' }    {[  50]}    {[   1 2 3]}
   {[3.1400]}    {'TEST'}    {3x3 double}

>> 

Each cell in a cell array is able to hold a different type of data string, number, matrix , array; this makes cell arrays flexible for handling heterogeneous data in MATLAB.

Convert Array to Cell Array

Here we will understand how to convert a given array into a cell array.We are going to make use of the function num2cell().

num2cell()

The num2cell() allows you to convert a numeric array into a cell array where each element of the numeric array becomes an element of the cell array.

Example

nArr = [1, 2, 3, 4, 5];
C = num2cell(nArr)

On execution in Matlab command window the output is −

>> nArr = [1, 2, 3, 4, 5];
C = num2cell(nArr)

C =
{
  [1,1] = 1
  [1,2] = 2
  [1,3] = 3
  [1,4] = 4
  [1,5] = 5
}

>> 

In this example, the num2cell() function converted the given numeric array [1, 2, 3, 4, 5] into a cell array where each element of the numeric array became an element of the cell array. The cell array C has the same size as the given numeric array, and each cell contains one element from the numeric array.

Convert Table to Cell Array

In this section will see an example that will show how you can convert a table to a cell array.

Matlab comes with a function called table2cell() that helps to convert a table to a cell array. While doing so each variable inside the table becomes the column of the cells.

table2cell()

The table2cell() function is used to convert a table into a cell array. Tables and cell arrays are two different data structures in MATLAB, with tables being useful for storing and managing tabular data with named columns, while cell arrays are more general-purpose containers that can hold different types of data in different cells.

Converting a table to a cell array using table2cell() can be useful when you want to perform operations that are more easily done with cell arrays.

Example

Name = {'Riya'; 'Siya'; 'Tiya'};
Age = [25; 30; 28];
Height = [160; 175; 168];
Weight = [55; 70; 65];
T = table(Name, Age, Height, Weight);

C = table2cell(T)

In the above example,we first create a sample table T with columns for Name, Age, Height, and Weight. Then, we use the table2cell() function to convert the table T into a cell array C. Finally, we display the contents of the cell array in C.

When you execute the same in matlab command window the output is −

>> Name = {'Riya'; 'Siya'; 'Tiya'};
Age = [25; 30; 28];
Height = [160; 175; 168];
Weight = [55; 70; 65];
T = table(Name, Age, Height, Weight);

C = table2cell(T)

C =

  3x4 cell array

   {'Riya'}    {[25]}    {[160]}    {[55]}
   {'Siya'}    {[30]}    {[175]}    {[70]}
   {'Tiya'}    {[28]}    {[168]}    {[65]}

>> 
Advertisements