Wednesday, July 3, 2024

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 Int
int[] intArray = {1, 2, 3};
 
// Creating an Array of type Float
float[] floatArray = {1, 2.5f, 3};
 
// Creating an Array of type Object
Object[] objectArray = {1, 2.3, "3"};


Output: 
 

Julia-Cell-Array-01

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 Int
a = [1, 2, 3]
     
# push an Int into the Array
push!(a, 5)
  
# So the type <strong>Int</strong> is retained
 
# Push a Float value into the Array
push!(a, 5.2)


Java




import java.util.ArrayList;
 
// Creating an ArrayList of type Integer
ArrayList<Integer> a = new ArrayList<>(Arrays.asList(1, 2, 3));
 
// Push an Integer into the ArrayList
a.add(5);
 
// So the type Integer is retained
 
// Push a Float value into the ArrayList
a.add(5.2f);


Output: 
 

Julia-Cell-Array-02

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 Int
a = [1, 2, 3]
     
# push an Int into the Array
push!(a, 5)
 
# Let's try to push the Float value now
push!!(a, 5.2)


Java




import java.util.ArrayList;
 
// Creating an ArrayList of type Integer
ArrayList<Number> a = new ArrayList<>(Arrays.asList(1, 2, 3));
 
// Push an Integer into the ArrayList
a.add(5);
 
// Let's try to push the Float value now
a.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 Int
a = [1, 2, 3]
     
# push an Int into the Array
push!(a, 5)
 
# Pushing the Float value
push!!(a, 5.2)
 
# Let's try to store a different data type now
push!!(a, "GeeksForGeeks")


Java




import java.util.ArrayList;
 
// Creating an ArrayList of type Object
ArrayList<Object> a = new ArrayList<>(Arrays.asList(1, 2, 3));
 
// Push an Integer into the ArrayList
a.add(5);
 
// Pushing the Float value
a.add(5.2f);
 
// Let's try to store a different data type now
a.add("GeeksForGeeks");


Output: 
 

Julia-Cell-Array-04

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 Integer
List<List<Integer>> a = Arrays.asList(
    Arrays.asList(1, 2),
    Arrays.asList(3, 4)
);
 
// Create a 2x2 List of type Float
List<List<Float>> b = Arrays.asList(
    Arrays.asList(1f, 2f),
    Arrays.asList(3f, 4.2f)
);
 
// Create a 2x2 List of type Object
List<List<Object>> c = Arrays.asList(
    Arrays.asList(1, 2),
    Arrays.asList(3, "foo")
);


Output: 
 

Julia-Cell-Array-05

Cell Arrays of 3D array: 
 

Python3




# Create a 2x2x2 Array of type Int
cat([1 2; 3 4], [5 6; 7 8], dims=3)
    
# Create a 2x2x2 Cell Array, of type Any
cat([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 Integer
List<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 Object
List<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: 
 

Julia-Cell-Array-06

 

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 Any
c = [[10 20 "GeeksForGeeks"], [40.0], [50 60]]
    
# push a row Cell Array, of type Any
c = push!(c, [1 2 10])


Java




import java.util.ArrayList;
 
// Create 3 element List, that contains sub-lists of type Object
List<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 Object
c.add(Arrays.asList(1, 2, 10));


Output: 
 

Julia-Cell-Array-07

Storing Cell Arrays within Cell Arrays:
 

Python3




# Create two, 3 element Cell Arrays i and j
i = [2, 3.0, "GeeksForGeeks"]
j = [2.3, 3.3, "CellArrays"]
     
# Create a 3x2 Array
IJ = [i j]


Java




import java.util.Arrays;
 
// Create two, 3 element Lists
List<Object> i = Arrays.asList(2, 3.0, "GeeksForGeeks");
List<Object> j = Arrays.asList(2.3, 3.3, "CellArrays");
 
// Create a 3x2 List
List<List<Object>> ij = Arrays.asList(i, j);


Output: 
 

Julia-Cell-Array-08

 

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 Arrays
i = [2, 3, 4]
j = [2.3, 3.3, 4.3]
 
# A function that uses values
# from the above two arrays
function FOO(i, j)
    a = i + j
end
     
# Calling the function
FOO(i, j)


Java




import java.util.Arrays;
 
// Create two ArrayLists
List<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 ArrayLists
public 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 function
List<Number> result = foo(i, j);


Output: 
 

Julia-Cell-Array-09

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.

 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Nango Kalahttps://www.kala.co.za
Experienced Support Engineer with a demonstrated history of working in the information technology and services industry. Skilled in Microsoft Excel, Customer Service, Microsoft Word, Technical Support, and Microsoft Office. Strong information technology professional with a Microsoft Certificate Solutions Expert (Privet Cloud) focused in Information Technology from Broadband Collage Of Technology.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments