Array creation using Comprehensions and Generators in Julia

Julia is a language designed for high-level performance and can support interactive use as well. It has many descriptive datatypes and type-declarations can be used to solidify the programs. Julia is slowly climbing the ladder and gaining the interest of many Data Scientists and machine learning scientists today. It is comparatively faster than Python as it is designed to implement the math concepts like linear algebra and matrix representations. Excellent for numerical computation, this language has multiple dispatches for defining data types like numbers and arrays.

Arrays using Comprehensions

Julia arrays use square brackets([ ]) for list comprehensions just like Python or MATLAB. It consists of three kinds of arrays. Array comprehension is a very powerful way to construct an array. The resulting array depends on the type of data used while construction.
Syntax:

[expression for element = iterable]

 
For 1-D Arrays:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Julia array using for loop
twice = []
for i in 1:5
    push!(twice, 2i)

chevron_right


can also be written and will give the same output as

filter_none

edit
close

play_arrow

link
brightness_4
code

# Using Comprehension
twice = [2x for x=1:5]

chevron_right



 
For 2-D Arrays:



filter_none

edit
close

play_arrow

link
brightness_4
code

# Creating 2D array using comprehension
  
u = [x + 2y for x in 1:5, y in 0:1]

chevron_right



 
For 3-D Arrays:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Creating 3D arrays using comprehension
  
p = [x + 2y + 3z for x in 1:4, y in 0:1, z in 1:3]

chevron_right


Arrays using Generators

This does not exactly return an array type data structure but a generator type data structure. It uses parentheses ‘( )’ instead of the square brackets as in list comprehensions. The syntax else is very similar to that of the above.
The object can be iterated to produce values when needed instead of allocating an array and storing them in advance. The series of data below are not allocated any memory unlike the examples above.

Syntax:

(expression for element = iterable)

 
For 1-D Arrays:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Creating 1D Array using Generators
  
j = (2x for x = 1:5)

chevron_right



 
For 2-D Arrays:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Creating 2D Arrays using Generators
u = (x + 2y for x in 1:5, y in 0:1)

chevron_right



 
For 3-D Arrays:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Creating 3D arrays using Generators
  
p = (x + 2y + 3z for x in 1:4, y in 0:1, z in 1:3)

chevron_right





My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.