Cell Arrays in Julia

Cell array is an abstract data type with indexed data containers called cells, where each cell can contain any type of data. It is normally used in Matlab to store data.
Coming to Julia, one of the magical things about Julia is its type system. It is a strictly typed language. In Julia, arrays can contain values of homogeneous [1, 2, 3] or heterogeneous types [1, 2.5, “3”]. Julia will try to promote the values to a common concrete type by default. If Julia can not promote the types contained, the resulting array would be of the abstract type Any.
Examples:
Python3
# Creating an Array of type Int [1, 2, 3]# Creating an Array of type Float[1, 2.5, 3]# Creating an Array of type Any[1, 2.3, "3"] |
Java
// Creating an Array of type Intint[] intArray = {1, 2, 3};// Creating an Array of type Floatfloat[] floatArray = {1, 2.5f, 3};// Creating an Array of type ObjectObject[] objectArray = {1, 2.3, "3"}; |
Output:
So it can be said that an Array{Any} is equivalent to a Matlab cell array. It can hold elements of different Data Types.
Creating a cell array from a regular array
A cell array can be created with a regular array with the use of some pre-defined methods in Julia.
Let’s create an Array of type Int and see it’s conversion from a regular Array to a Cell Array!
Python3
# Creating an Array of type Inta = [1, 2, 3] # push an Int into the Arraypush!(a, 5) # So the type <strong>Int</strong> is retained# Push a Float value into the Arraypush!(a, 5.2) |
Java
import java.util.ArrayList;// Creating an ArrayList of type IntegerArrayList<Integer> a = new ArrayList<>(Arrays.asList(1, 2, 3));// Push an Integer into the ArrayLista.add(5);// So the type Integer is retained// Push a Float value into the ArrayLista.add(5.2f); |
Output:
Above code generates an error when we try to push a float value in an Integer typed Array. This shows that Julia is strictly typed and doesn’t allow pushing elements of any other data type.
Changing Data type of an Array: Julia by default doesn’t change the type of an Array and hence it doesn’t allow pushing differently typed value in the array. This can be avoided with the use of external packages.
Example:
Python3
# A special package to allow extended operations with push!()# Install it by typing "add BangBang"using BangBang# Creating an Array of type Inta = [1, 2, 3] # push an Int into the Arraypush!(a, 5)# Let's try to push the Float value nowpush!!(a, 5.2) |
Java
import java.util.ArrayList;// Creating an ArrayList of type IntegerArrayList<Number> a = new ArrayList<>(Arrays.asList(1, 2, 3));// Push an Integer into the ArrayLista.add(5);// Let's try to push the Float value nowa.add(5.2f); |
Output:
Above code converts the Array to Float64, similarly, it can be used to convert the same to a string array, etc.
Example:
Python3
using BangBang# Creating an Array of type Inta = [1, 2, 3] # push an Int into the Arraypush!(a, 5)# Pushing the Float valuepush!!(a, 5.2)# Let's try to store a different data type nowpush!!(a, "GeeksForGeeks") |
Java
import java.util.ArrayList;// Creating an ArrayList of type ObjectArrayList<Object> a = new ArrayList<>(Arrays.asList(1, 2, 3));// Push an Integer into the ArrayLista.add(5);// Pushing the Float valuea.add(5.2f);// Let's try to store a different data type nowa.add("GeeksForGeeks"); |
Output:
This converts a regular Array into a cell Array which holds values of heterogeneous data types.
Cell Arrays of 2D Arrays:
Python3
# Create a 2x2 Array of type Int[1 2 ; 3 4] # Create a 2x2 Array of type Float[1 2 ; 3 4.2] # Create a 2x2 Cell Array, of type Any[1 2 ; 3 "foo"] |
Java
// Create a 2x2 List of type IntegerList<List<Integer>> a = Arrays.asList( Arrays.asList(1, 2), Arrays.asList(3, 4));// Create a 2x2 List of type FloatList<List<Float>> b = Arrays.asList( Arrays.asList(1f, 2f), Arrays.asList(3f, 4.2f));// Create a 2x2 List of type ObjectList<List<Object>> c = Arrays.asList( Arrays.asList(1, 2), Arrays.asList(3, "foo")); |
Output:
Cell Arrays of 3D array:
Python3
# Create a 2x2x2 Array of type Intcat([1 2; 3 4], [5 6; 7 8], dims=3) # Create a 2x2x2 Cell Array, of type Anycat([1 "foo"; 3.2 4], [5 "baz"; 7 8], dims=3) |
Java
import java.util.ArrayList;import java.util.List;// Create a 2x2x2 List of type IntegerList<List<List<Integer>>> a = new ArrayList<>();a.add(Arrays.asList( Arrays.asList(1, 2), Arrays.asList(3, 4)));a.add(Arrays.asList( Arrays.asList(5, 6), Arrays.asList(7, 8)));// Create a 2x2x2 List of type ObjectList<List<List<Object>>> b = new ArrayList<>();b.add(Arrays.asList( Arrays.asList(1, "foo"), Arrays.asList(3.2, 4)));b.add(Arrays.asList( Arrays.asList(5, "baz"), Arrays.asList(7, 8))); |
Output:
Adding rows to a Cell Array
Additional rows can be added to the end of the cell array with the use of push!() function.
Python3
# Create 3 element Cell Array, # that contains sub-arrays of type Anyc = [[10 20 "GeeksForGeeks"], [40.0], [50 60]] # push a row Cell Array, of type Anyc = push!(c, [1 2 10]) |
Java
import java.util.ArrayList;// Create 3 element List, that contains sub-lists of type ObjectList<List<Object>> c = new ArrayList<>(Arrays.asList( Arrays.asList(10, 20, "GeeksForGeeks"), Arrays.asList(40.0), Arrays.asList(50, 60)));// push a row List, of type Objectc.add(Arrays.asList(1, 2, 10)); |
Output:
Storing Cell Arrays within Cell Arrays:
Python3
# Create two, 3 element Cell Arrays i and ji = [2, 3.0, "GeeksForGeeks"]j = [2.3, 3.3, "CellArrays"] # Create a 3x2 ArrayIJ = [i j] |
Java
import java.util.Arrays;// Create two, 3 element ListsList<Object> i = Arrays.asList(2, 3.0, "GeeksForGeeks");List<Object> j = Arrays.asList(2.3, 3.3, "CellArrays");// Create a 3x2 ListList<List<Object>> ij = Arrays.asList(i, j); |
Output:
Use of Cell Arrays
A cell array is just like a regular Julia array. They are used across various applications, as the representation of DataFrames, Tuples, etc.
Cell Arrays in Julia are also used to store input arguments for a function.
Python3
# Create two Arraysi = [2, 3, 4]j = [2.3, 3.3, 4.3]# A function that uses values # from the above two arraysfunction FOO(i, j) a = i + jend # Calling the functionFOO(i, j) |
Java
import java.util.Arrays;// Create two ArrayListsList<Integer> i = Arrays.asList(2, 3, 4);List<Double> j = Arrays.asList(2.3, 3.3, 4.3);// A function that uses values from the above two ArrayListspublic static List<Number> foo(List<Integer> i, List<Double> j) { List<Number> a = new ArrayList<>(); for (int k = 0; k < i.size(); k++) { a.add(i.get(k) + j.get(k)); } return a;}// Calling the functionList<Number> result = foo(i, j); |
Output:
Advantages of Cell Arrays:
- They allow for greater flexibility to represent data.
- Instead of creating different arrays for a different types, they provide the space-efficient way to store all types in one array.
- They provide a unique data type “Any“that allows all sorts of Data Types.
Disadvantages of Cell Arrays:
- They are very slow. Julia is a strictly types language(means that the types are pre-defined)
- We lose information about the data, eg: When we push a String value into a Float array to convert it to a Cell Array, it changes from Array{Float64} to Array{Any} type, hence the information about data types in the Cell Array is lost.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!




