Skip to content
Related Articles

Related Articles

Improve Article

Cell Arrays in Julia

  • Last Updated : 22 Apr, 2020

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:




# 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"]

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!




# 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)

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:




# 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)

Output:

Above code converts the Array to Float64, similarly, it can be used to convert the same to a string array, etc.

Example:






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")

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:




# 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"]

Output:
Julia-Cell-Array-05

Cell Arrays of 3D array:




# 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)

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.




# 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])

Output:
Julia-Cell-Array-07

Storing Cell Arrays within Cell Arrays:




# 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]

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.




# 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)

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.

Disavantages of Cell Arrays:

  • They are very slow. Julia is a strictly types language(means tha 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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :