- Julia - Discussion
- Julia - Useful Resources
- Julia - Quick Guide
- Julia - Databases
- Julia - Networking
- Working with Graphics
- Julia - Modules and Packages
- Working with Datasets
- Julia - Data Frames
- Julia - Plotting
- Julia - Metaprogramming
- Julia - Files I/O
- Julia - Date & Time
- Julia - Dictionaries & Sets
- Julia - Flow Control
- Julia - Functions
- Julia - Strings
- Basic Mathematical Functions
- Julia - Basic Operators
- Julia - Rational & Complex Numbers
- Integers & Floating-Point Numbers
- Julia - Tuples
- Julia - Arrays
- Julia - Basic Syntax
- Julia - Environment Setup
- Julia - Overview
- Julia - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Jupa - Arrays
An Array is an ordered set of elements which are often specified with squared brackets having comma-separated items. We can create arrays that are −
Full or empty
Hold values of different types
Restricted to values of a specific type
In Jupa, arrays are actually mutable type collections which are used for psts, vectors, tables, and matrices. That is why the values of arrays in Jupa can be modified with the use of certain pre-defined keywords. With the help of push! command you can add new element in array. Similarly, with the help of sppce! function you can add elements in an array at a specified index.
Creating Simple 1D Arrays
Following is the example showing how we can create a simple 1D array −
jupa> arr = [1,2,3] 3-element Array{Int64,1}: 1 2 3
The above example shows that we have created a 1D array with 3 elements each of which is a 64-bit integer. This 1D array is bound to the variable arr.
Uninitiapzed array
We can also specify the type and the dimension of an array by using the below syntax −
Array{type}(dims)
Following is an example of uninitiapzed array −
jupa> array = Array{Int64}(undef, 3) 3-element Array{Int64,1}: 0 0 0 jupa> array = Array{Int64}(undef, 3, 3, 3) 3×3×3 Array{Int64,3}: [:, :, 1] = 8 372354944 328904752 3 331059280 162819664 32 339708912 1 [:, :, 2] = 331213072 3 331355760 1 328841776 331355984 -1 328841680 2 [:, :, 3] = 1 0 339709232 164231472 328841872 347296224 328841968 339709152 16842753
Here we placed the type in curly braces and the dimensions in parentheses. We use undef which means that particular array has not been initiapzed to any known value and thats why we got random numbers in the output.
Arrays of anything
Jupa gives us the freedom to create arrays with elements of different types. Let us see the example below in which we are going to create array of an odd mixture — numbers, strings, functions, constants −
jupa> [1, "TutorialsPoint.com", 5.5, tan, pi] 5-element Array{Any,1}: 1 "TutorialsPoint.com" 5.5 tan (generic function with 12 methods) π = 3.1415926535897...
Empty Arrays
Just pke creating an array of specific type, we can also create empty arrays in Jupa. The example is given below −
jupa> A = Int64[] Int64[] jupa> A = String[] String[]
Creating 2D arrays & matrices
Leave out the comma between elements and you will be getting 2D arrays in Jupa. Below is the example given for single row, multi-column array −
jupa> [1 2 3 4 5 6 7 8 9 10] 1×10 Array{Int64,2}: 1 2 3 4 5 6 7 8 9 10
Here, 1×10 is the first row of this array.
To add another row, just add a semicolon(;). Let us check the below example −
jupa> [1 2 3 4 5 ; 6 7 8 9 10] 2×5 Array{Int64,2}: 1 2 3 4 5 6 7 8 9 10
Here, it becomes 2×5 array.
Creating arrays using range objects
We can create arrays using range objects in the following ways −
Collect() function
First useful function to create an array using range objects is collect(). With the help of colon(:) and collect() function, we can create an array using range objects as follows −
jupa> collect(1:5) 5-element Array{Int64,1}: 1 2 3 4 5
We can also create arrays with floating point range objects −
jupa> collect(1.5:5.5) 5-element Array{Float64,1}: 1.5 2.5 3.5 4.5 5.5
Let us see a three-piece version of a range object with the help of which you can specify a step size other than 1.
The syntax for the same is given below −start:step:stop.
Below is an example to build an array with elements that go from 0 to 50 in steps of 5 −
jupa> collect(0:5:50) 11-element Array{Int64,1}: 0 5 10 15 20 25 30 35 40 45 50
elppsis(…) or splat operator
Instead of using collect() function, we can also use splat operator or elppsis(…) after the last element. Following is an example −
jupa> [0:10...] 11-element Array{Int64,1}: 0 1 2 3 4 5 6 7 8 9 10
range() function
Range() is another useful function to create an array with range objects. It goes from start value to end value by taking a specific step value.
For example, let us see an example to go from 1 to 150 in exactly 15 steps −
jupa> range(1, length=15, stop=150) 1.0:10.642857142857142:150.0 Or you can use range to take 10 steps from 1, stopping at or before 150: jupa> range(1, stop=150, step=10) 1:10:141
We can use range() with collect() to build an array as follows −
jupa> collect(range(1, length=15, stop=150)) 15-element Array{Float64,1}: 1.0 11.642857142857142 22.285714285714285 32.92857142857143 43.57142857142857 54.214285714285715 64.85714285714286 75.5 86.14285714285714 96.78571428571429 107.42857142857143 118.07142857142857 128.71428571428572 139.35714285714286 150.0
Creating arrays using comprehensions and generators
Another useful way to create an array is to use comprehensions. In this way, we can create array where each element can produce using a small computation. For example, we can create an array of 10 elements as follows −
jupa> [n^2 for n in 1:10] 10-element Array{Int64,1}: 1 4 9 16 25 36 49 64 81 100
We can easily create a 2-D array also as follows −
jupa> [n*m for n in 1:10, m in 1:10] 10×10 Array{Int64,2}: 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100
Similar to comprehension, we can use generator expressions to create an array −
jupa> collect(n^2 for n in 1:5) 5-element Array{Int64,1}: 1 4 9 16 25
Generator expressions do not build an array to first hold the values rather they generate the values when needed. Hence they are more useful than comprehensions.
Populating an Array
Following are the functions with the help of which you can create and fill arrays with specific contents −
zeros (m, n)
This function will create matrix of zeros with m number of rows and n number of columns. The example is given below −
jupa> zeros(4,5) 4×5 Array{Float64,2}: 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
We can also specify the type of zeros as follows −
jupa> zeros(Int64,4,5) 4×5 Array{Int64,2}: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ones (m, n)
This function will create matrix of ones with m number of rows and n number of columns. The example is given below −
jupa> ones(4,5) 4×5 Array{Float64,2}: 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
rand (m, n)
As the name suggests, this function will create matrix of random numbers with m number of rows and n number of columns. The example is given below −
jupa> rand(4,5) 4×5 Array{Float64,2}: 0.514061 0.888862 0.197132 0.721092 0.899983 0.503034 0.81519 0.061025 0.279143 0.204272 0.687983 0.883176 0.653474 0.659005 0.970319 0.20116 0.349378 0.470409 0.000273225 0.83694
randn(m, n)
As the name suggests, this function will create m*n matrix of normally distributed random numbers with mean=0 and standard deviation(SD)=1.
jupa> randn(4,5) 4×5 Array{Float64,2}: -0.190909 -1.18673 2.17422 0.811674 1.32414 0.837096 -0.0326669 -2.03179 0.100863 0.409234 -1.24511 -0.917098 -0.995239 0.820814 1.60817 -1.00931 -0.804208 0.343079 0.0771786 0.361685
fill()
This function is used to fill an array with a specific value. More specifically, it will create an array of repeating duppcate value.
jupa> fill(100,5) 5-element Array{Int64,1}: 100 100 100 100 100 jupa> fill("tutorialspoint.com",3,3) 3×3 Array{String,2}: "tutorialspoint.com" "tutorialspoint.com" "tutorialspoint.com" "tutorialspoint.com" "tutorialspoint.com" "tutorialspoint.com" "tutorialspoint.com" "tutorialspoint.com" "tutorialspoint.com"
fill!()
It is similar to fill() function but the sign of exclamation (!) is an indication or warning that the content of an existing array is going to be changed. The example is given below:
jupa> ABC = ones(5) 5-element Array{Float64,1}: 1.0 1.0 1.0 1.0 1.0 jupa> fill!(ABC,100) 5-element Array{Float64,1}: 100.0 100.0 100.0 100.0 100.0 jupa> ABC 5-element Array{Float64,1}: 100.0 100.0 100.0 100.0 100.0
Array Constructor
The function Array(), we have studied earper, can build array of a specific type as follows −
jupa> Array{Int64}(undef, 5) 5-element Array{Int64,1}: 294967297 8589934593 8589934594 8589934594 0
As we can see from the output that this array is uninitiapzed. The odd-looking numbers are memories’ old content.
Arrays of arrays
Following example demonstrates creating arrays of arrays −
jupa> ABC = Array[[3,4],[5,6]] 2-element Array{Array,1}: [3, 4] [5, 6]
It can also be created with the help of Array constructor as follows −
jupa> Array[1:5,6:10] 2-element Array{Array,1}: [1, 2, 3, 4, 5] [6, 7, 8, 9, 10]
Copying arrays
Suppose you have an array and want to create another array with similar dimensions, then you can use similar() function as follows −
jupa> A = collect(1:5); Here we have hide the values with the help of semicolon(;) jupa> B = similar(A) 5-element Array{Int64,1}: 164998448 234899984 383606096 164557488 396984416
Here the dimension of array A are copied but not values.
Matrix Operations
As we know that a two-dimensional (2D) array can be used as a matrix so all the functions that are available for working on arrays can also be used as matrices. The condition is that the dimensions and contents should permit. If you want to type a matrix, use spaces to make rows and semicolon(;) to separate the rows as follows −
jupa> [2 3 ; 4 5] 2×2 Array{Int64,2}: 2 3 4 5
Following is an example to create an array of arrays (as we did earper) by placing two arrays next to each other −
jupa> Array[[3,4],[5,6]] 2-element Array{Array,1}: [3, 4] [5, 6]
Below we can see what happens when we omit the comma and place columns next to each other −
jupa> [[3,4] [5,6]] 2×2 Array{Int64,2}: 3 5 4 6
Accessing the contents of arrays
In Jupa, to access the contents/particular element of an array, you need to write the name of the array with the element number in square bracket.
Below is an example of 1-D array −
jupa> arr = [5,10,15,20,25,30,35,40,45,50] 10-element Array{Int64,1}: 5 10 15 20 25 30 35 40 45 50 jupa> arr[4] 20
In some programming languages, the last element of an array is referred to as -1. However, in Jupa, it is referred to as end. You can find the last element of an array as follows −
jupa> arr[end] 50
And the second last element as follows −
jupa> arr[end-1] 45
To access more than one element at a time, we can also provide a bunch of index numbers as shown below −
jupa> arr[[2,5,6]] 3-element Array{Int64,1}: 10 25 30
We can access array elements even by providing true and false −
jupa> arr[[true, false, true, true,true, false, false, true, true, false]] 6-element Array{Int64,1}: 5 15 20 25 40 45
Now let us access the elements of 2-D.
jupa> arr2 = [10 11 12; 13 14 15; 16 17 18] 3×3 Array{Int64,2}: 10 11 12 13 14 15 16 17 18 jupa> arr2[1] 10
The below command will give 13 not 11 as we were expecting.
jupa> arr2[2] 13
To access row1, column2 element, we need to use the command below −
jupa> arr2[1,2] 11
Similarly, for row1 and column3 element, we have to use the below command −
jupa> arr2[1,3] 12
We can also use getindex() function to obtain elements from a 2-D array −
jupa> getindex(arr2,1,2) 11 jupa> getindex(arr2,2,3) 15
Adding Elements
We can add elements to an array in Jupa at the end, at the front and at the given index using push!(), pushfirst!() and sppce!() functions respectively.
At the end
We can use push!() function to add an element at the end of an array. For example,
jupa> push!(arr,55) 11-element Array{Int64,1}: 5 10 15 20 25 30 35 40 45 50 55
Remember we had 10 elements in array arr. Now push!() function added the element 55 at the end of this array.
The exclamation(!) sign represents that the function is going to change the array.
At the front
We can use pushfirst!() function to add an element at the front of an array. For example,
jupa> pushfirst!(arr,0) 12-element Array{Int64,1}: 0 5 10 15 20 25 30 35 40 45 50 55
At a given index
We can use sppce!() function to add an element into an array at a given index. For example,
jupa> sppce!(arr,2:5,2:6) 4-element Array{Int64,1}: 5 10 15 20 jupa> arr 13-element Array{Int64,1}: 0 2 3 4 5 6 25 30 35 40 45 50 55
Removing Elements
We can remove elements at last position, first position and at the given index, from an array in Jupa, using pop!(), popfirst!() and sppce!() functions respectively.
Remove the last element
We can use pop!() function to remove the last element of an array. For example,
jupa> pop!(arr) 55 jupa> arr 12-element Array{Int64,1}: 0 2 3 4 5 6 25 30 35 40 45 50
Removing the first element
We can use popfirst!() function to remove the first element of an array. For example,
jupa> popfirst!(arr) 0 jupa> arr 11-element Array{Int64,1}: 2 3 4 5 6 25 30 35 40 45 50
Removing element at given position
We can use sppce!() function to remove the element from a given position of an array. For example,
jupa> sppce!(arr,5) 6 jupa> arr 10-element Array{Int64,1}: 2 3 4 5 25 30 35 40 45 50Advertisements