An array is an indexable collection of (normally) homogeneous values such as integers, floats, booleans. In Julia, unlike many programming languages, the index starts at 1
not 0
.
One simple way to create an array is to enumerate its values:
julia> A = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]; 15-element Array{Int64,1}
These are the first 15 values of the Fibonacci series and because all values are listed as integers the array created is of type Int64
. The other number refers to the number of dimensions of the array, in this case 1.
In conjunction of loops in the Asian option example in the previous chapter, we meet the definition of a range as: start:[step]:end
julia> A = [1:10]; B = [1:3:15]; C =[1:0.5:5];
Here A
is [1,2,3,4,5,6,7,8,9,10]
, B
is [1,4,7,10,13]
and C is [1.0,1.5,2.0,2.5,3.0,3.5,4.0,4.5,5.0]
Because the step in C
is specified as a float value the array is of type Float64
not Int64
.
Julia also provides functions such as zeros
, ones
and rand
which provide...