Julia Arrays
An array is a collection of elements of the same data type arranged in a specific order, which can be a one-dimensional or multi-dimensional array.
Julia supports the array data structure, which can store a collection of elements that are not fixed in size and can be of the same or different types.
Julia arrays are mutable collections used for lists, vectors, tables, and matrices.
The index keys of Julia arrays can be represented by integers, and the size of the array is not fixed.
Julia provides many functions to help us manipulate arrays, such as adding elements to an array, merging arrays, etc.
Julia arrays are specified using square brackets [...]
, and multiple elements are separated by commas ,
.
The syntax for creating a one-dimensional array (i.e., a vector) is:
[A, B, C, ...]
Creating a One-Dimensional Array
The following example creates a simple one-dimensional array:
Example
julia> arr = [1, 2, 3]
3-element Vector{Int64}:
1
2
3
In the above example, we created a one-dimensional array with 3 elements, each being a 64-bit integer, and this array is bound to the variable arr
.
The types of array elements can also be different:
Example
julia> arr = [1, "tutorialpro", 2.5, pi]
4-element Vector{Any}:
1
"tutorialpro"
2.5
π = 3.1415926535897...
In the above example, we created a one-dimensional array with 4 elements of different types, where pi is the constant π, and this array is bound to the variable arr
.
You can also specify the type explicitly:
Example
julia> arr = Int64[1, 2, 3]
3-element Vector{Int64}:
1
2
3
julia> arr2 = String["Taobao", "tutorialpro", "GOOGLE"]
3-element Vector{String}:
"Taobao"
"tutorialpro"
"GOOGLE"
In the above examples, array arr is restricted to integers only, and arr2 is restricted to strings only.
We can also create an empty array:
Example
julia> arr = Int64[]
Int64[]
julia> arr2 = String[]
String[]
Arrays can be accessed directly using index values; the first value has an index of 1 (not 0), the second value has an index of 2, and so on. The last value can be represented by end
:
Example
julia> arr = Int64[1, 2, 3]
3-element Vector{Int64}:
1
2
3
julia> arr[2]
2
julia> arr2 = String["Taobao", "tutorialpro", "GOOGLE"]
3-element Vector{String}:
"Taobao"
"tutorialpro"
"GOOGLE"
julia> arr2[1]
"Taobao"
julia> arr2[end]
"GOOGLE"
Specifying Array Type and Dimensions
We can also specify the type and dimensions of an array using the following syntax:
Array{type}(undef, dims...)
undef indicates that the array is uninitialized.
dims... can be a single tuple of dimensions or a set of dimensions as variable arguments.
dims... numbers represent the number of elements, and multiple dimensions are separated by commas ,
.
Example
julia> array = Array{Int64}(undef, 3) # Represents a one-dimensional array with 3 elements
3-element Vector{Int64}:
4834342704
4377305096
0
julia> array = Array{Int64}(undef, 3, 3, 3) # Represents a 3-dimensional array with 3 elements in each dimension
3×3×3 Array{Int64, 3}:
[:, :, 1] =
4562265712 0 0
1 0 0
0 0 0
[:, :, 2] =
0 0 0
0 0 0
0 0 0
[:, :, 3] =
0 0 0
0 0 0
0 0 0
In the above examples, the type of the array is placed in curly braces {}
, and undef
is used to set the array to be uninitialized to any known value, which is why we get random numbers in the output.
Creating Two-Dimensional Arrays and Matrices
We can omit the commas ,
between array elements or use double semicolons ;;
to create a two-dimensional array, as shown in the following example:
Example
julia> [1 2 3 4]
1×4 Matrix{Int64}:
1 2 3 4
julia> [1;; 2;; 3;; 4]
1×4 Matrix{Int64}:
1 2 3 4
This is a Chinese to English translation, please provide the English translation for this text. Do not provide any explanations or text apart from the translation. Chinese: 1 2 3 4
**Note:** The first line output `1×4 Matrix{Int64}:`, 1x4 indicates a one-row, four-column matrix.
Although it's only one row, it's still a two-dimensional array because Julia only recognizes column vectors, not so-called row vectors.
To add another row, simply add a semicolon `;`, see the following example:
## Example
julia> [1 2; 3 4] 2×2 Matrix{Int64}: 1 2 3 4
You can also use a colon `:` and space ` ` to achieve this, see the following example:
## Example
julia> [1:2 3:4] 2×2 Matrix{Int64}: 1 3 2 4
**Note:** The first line output `2×2 Matrix{Int64}:`, 2×2 indicates a two-row, two-column matrix.
We can also create a two-dimensional array by embedding multiple one-dimensional arrays of the same length within square brackets `[]` and separating them with spaces:
## Example
julia> [[1,2] [3,4] [5,6]] 2×3 Matrix{Int64}: 1 3 5 2 4 6
**2x3** indicates a two-row, three-column array.
Below, we create a two-row, three-column and a three-row, two-column two-dimensional array by flexibly using semicolons `;` and spaces ` `:
## Example
julia> [[1;2] [3;4] [5;6]] 2×3 Matrix{Int64}: 1 3 5 2 4 6
julia> [[1 2]; [3 4]; [5 6]] 3×2 Matrix{Int64}: 1 2 3 4 5 6
---
## Creating Arrays Using Range Functions
### Ellipsis ...
You can use the ellipsis `...` to create an array, as shown in the following example:
## Example
julia> [0:10...] 11-element Vector{Int64}: 0 1 2 3 4 5 6 7 8 9 10
### collect() Function
The collect() function has the following syntax:
collect(start:step:stop)
start is the starting value, step is the step size, and stop is the ending value.
This function returns an array.
The following example has a start value of 1, a step size of 2, and an end value of 13:
## Example
julia> collect(1:2:13) 7-element Vector{Int64}: 1 3 5 7 9 11 13
The collect() function can also specify a type, with the following syntax:
collect(element_type, start:step:stop)
The following example creates an array of floating-point numbers:
## Example
julia> collect(Float64, 1:2:5) 3-element Vector{Float64}: 1.0 3.0 5.0
### range() Function
The range() function can generate a range with a specified step size, making it convenient for the collect() function to call.
The range() function has the following syntax:
start is the starting value, step is the step size, stop is the ending value, and length is the length.
## Example
julia> range(1, length=100) 1:100
julia> range(1, stop=100) 1:100
julia> range(1, step=5, length=100) 1:5:496
julia> range(1, step=5, stop=100) 1:5:96
julia> range(1, 10, length=101) 1.0:0.09:10.0
julia> range(1, 100, step=5) 1:5:96
julia> range(stop=10, length=5) 6:10
julia> range(stop=10, step=1, length=5) 6:1:10
julia> range(start=1, step=1, stop=10) 1:1:10
If the length is not specified, and `stop - start` is not an integer multiple of step, the range will end before stop.
julia> range(1, 3.5, step=2) 1.0:2.0:3.0
Using range() and collect() to create an array:
## Example
julia> collect(range(1,stop=10)) 10-element Vector{Int64}: 1 2 3 4 5 6 7 8 9 10 julia> collect(range(1, length=15, stop=150)) 15-element Vector{Float64}: 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
| copy(A) | Copy A |
| deepcopy(A) | Deep copy, i.e., copy A and recursively copy its elements |
| similar(A, T, dims...) | An uninitialized array with the same type (dense, sparse, etc.) as A, but with specified element type and dimensions. The second and third parameters are optional; if omitted, they default to the element type and dimensions of A. |
| reinterpret(T, A) | An array with the same binary data as A, but with element type T |
| rand(T, dims...) | A random Array with element values uniformly distributed in the half-open interval [0, 1) and independently identically distributed [1] |
| randn(T, dims...) | A random Array with elements normally distributed with mean 0 and standard deviation 1, independently identically distributed |
| Matrix{T}(I, m, n) | An identity matrix of m rows and n columns (requires using LinearAlgebra to use I) |
| range(start, stop=stop, length=n) | A range from start to stop with n linearly spaced elements |
| fill!(A, x) | Fill array A with value x |
| fill(x, dims...) | An Array filled with value x |
**zeros()** creates an array instance with all elements initialized to 0:
## Example
```julia
julia> zeros(Int8, 2, 3)
2×3 Matrix{Int8}:
0 0 0
0 0 0
julia> zeros(Int8, (2, 3))
2×3 Matrix{Int8}:
0 0 0
0 0 0
julia> zeros((2, 3))
2×3 Matrix{Float64}:
0.0 0.0 0.0
0.0 0.0 0.0