# Julia Programming 3 Array 1 Creation, Initialization, Properties and Access

Keywords: Programming

;

# Create Array

1. Create arrays by columns: If you create arrays in square brackets, and elements are separated by commas, the created arrays are in the form of column vectors; you can also create multidimensional arrays by columns in square brackets

```julia> a = [1,2,3,4]
4-element Array{Int64,1}:
1
2
3
4

julia> b = [[1,2,3] [4,5,6] [7,8,9]]
3×3 Array{Int64,2}:
1  4  7
2  5  8
3  6  9
```

2. Create arrays by rows: If you create arrays with square brackets and space between elements, the created arrays are in the form of row vectors;

```julia> b = [1 2 3 4]
1×4 Array{Int64,2}:
1  2  3  4
```

However, the dimensions of arrays created by these two methods are different, the first is one-dimensional and the second is two-dimensional.size() returns the number of rows and columns of the array, ndims() returns the dimension of the array:

```julia> size(a)
(4,)

julia> size(b)
(1, 4)

julia> ndims(a)
1

julia> ndims(b)
2
```

3. Using Array{data_The type, Number} (undef, Number) statement creates an array. Number needs to be Int64. Numbers in {} represent the dimensions of the array. Numbers in () represent the size, size, and dimensionality of the array. For example, a two-dimensional array needs two numbers to represent size. Arrays created with this statement are automatically filled with random numbers:

```julia> a = Array{Int16}(undef,3)
3-element Array{Int16,1}:
12
0
0

julia> a = Array{Int16,2}(undef,2,3)
2×3 Array{Int16,2}:
31712  0  -27632
5687  0    5264
```

4. Use zeros (data_The type, Number, Number) statement creates an array with all elements 0, and the number represents the size of the array;
Using ones (data_The type, Number, Number) statement creates an array with all elements 0, and the number also represents the size of the array;
Create an array with all elements true using a trues (Number, Number) statement. The number represents the size of the array, because true is Bool and does not need to specify a data type. Similarly, there is a falses (Number, Number) statement

```julia> b = zeros(Float64,3,3)
3×3 Array{Float64,2}:
0.0  0.0  0.0
0.0  0.0  0.0
0.0  0.0  0.0

julia> b1 = ones(Float64,3,3)
3×3 Array{Float64,2}:
1.0  1.0  1.0
1.0  1.0  1.0
1.0  1.0  1.0

julia> c1 = trues(3,3)
3×3 BitArray{2}:
1  1  1
1  1  1
1  1  1

julia> c2 = falses(3,3)
3×3 BitArray{2}:
0  0  0
0  0  0
0  0  0
```

5. Use rand(data_type,Number,Number) creates an evenly distributed random array between [0,1], with numbers representing the size of the array; randn(data_type,Number,Number) can create random arrays of standard normal distribution, where numbers represent the size of the array;

```julia> d1 = rand(Float64,3,3)
3×3 Array{Float64,2}:
0.110029  0.772143  0.434626
0.821364  0.592196  0.645199
0.1322    0.433895  0.0889052

julia> d2 = randn(Float64,3,3)
3×3 Array{Float64,2}:
0.779424   1.00182    0.0407997
-1.65429    0.502965  -0.55426
1.35664   -1.0813     0.869116
```

6. Use the statement fill!(A,x) to fill the entire array AAA with a value x x x; use the fill(x,Number,Number) statement to create an array where all values are x x x and Number determines size; the data type of the array after filling is determined by AAA, and the data type of the array after filling is determined by x x x

```julia> e1 = fill!(d1,1)
3×3 Array{Float64,2}:
1.0  1.0  1.0
1.0  1.0  1.0
1.0  1.0  1.0

julia> e2 = fill(0,3,3)
3×3 Array{Int64,2}:
0  0  0
0  0  0
0  0  0
```

7. Create a new array based on the existing array AAA, you can choose reshape, copy, similar methods:

```julia> A = randn(Float64,4,4)
4×4 Array{Float64,2}:
-0.380822   0.0876638  -0.560772   0.152027
-0.970889  -1.00651     1.78672    0.309796
0.802797   0.868499   -0.401544   1.23172
-0.423717   1.29024     1.45242   -0.703994

julia> B = copy(A)
4×4 Array{Float64,2}:
-0.380822   0.0876638  -0.560772   0.152027
-0.970889  -1.00651     1.78672    0.309796
0.802797   0.868499   -0.401544   1.23172
-0.423717   1.29024     1.45242   -0.703994

julia> reshape(A,8,2)
8×2 Array{Float64,2}:
-0.380822   -0.560772
-0.970889    1.78672
0.802797   -0.401544
-0.423717    1.45242
0.0876638   0.152027
-1.00651     0.309796
0.868499    1.23172
1.29024    -0.703994

julia> similar(A)
4×4 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
```

Copy method is simple, that is, copy the array AAA directly; similar method is to create an uninitialized array of the same numeric type, dimension, size and AAA; reshape is to change the array AAA into another shape, it is important to note that the number of elements under the new size is the same as that of AAA, otherwise error will occur; reshape is to flatten the AAA column by column and fill it into the new sizeWithin an array

```julia> reshape(A,2,2)
ERROR: DimensionMismatch("new dimensions (2, 2) must be consistent with array size 16")
Stacktrace:
[1] (::Base.var"#throw_dmrsa#196")(::Tuple{Int64,Int64}, ::Int64) at .\reshapedarray.jl:41
[2] reshape at .\reshapedarray.jl:45 [inlined]
[3] reshape(::Array{Float64,2}, ::Int64, ::Int64) at .\reshapedarray.jl:116
[4] top-level scope at none:0
```

8. Create an array of functions: The elements of the array are not necessarily numeric, but can also be functions, such as

```julia> a = [sin,cos,tan]
3-element Array{Function,1}:
sin
cos
tan

julia> a[1](pi)
1.2246467991473532e-16
```

9. Elements in arrays do not necessarily have the same type, such as

```julia> a = [sin,pi,2.2,4]
4-element Array{Any,1}:
sin
π
2.2
4

julia> a[1](a[2])
1.2246467991473532e-16
```

10. To create an array with s s s steps from a a a to B B b, you can use collect(a:s:b), where a\sb can not be an integer; ellipsis can replace the collect function, for example, the collect(a:s:b) statement can use [a:s:b...]instead

```julia> collect(1:2:9)
5-element Array{Int64,1}:
1
3
5
7
9

julia> collect(1.2:0.2:3.3)
11-element Array{Float64,1}:
1.2
1.4
1.6
1.8
2.0
2.2
2.4
2.6
2.8
3.0
3.2

julia> [1.2:0.2:3.3...]
11-element Array{Float64,1}:
1.2
1.4
1.6
1.8
2.0
2.2
2.4
2.6
2.8
3.0
3.2
```

# Properties of arrays

The first five are all fairly understandable. Here's an explanation of the last five.The axes method is an indicator feature that returns an array, such as

```julia> A = zeros(3,4)
3×4 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

julia> axes(A)
(Base.OneTo(3), Base.OneTo(4))

julia> axes(A,1)
Base.OneTo(3)

julia> axes(A,2)
Base.OneTo(4)
```

Array A dimension is 2, size is 3,4,Base.OneTo() Indicator is a number from 1 to parentheses, returned by axes (A, 1)Base.OneTo(3) Indicators for rows representing A range from 1 to 3.eachindex is also an indicator feature that returns arrays, but it is an indicator that arranges array elements in columns. A has 12 elements, so the indicator is 1 to 12

```julia> eachindex(A)
Base.OneTo(12)
```

The string method returns the index distance of a dimension adjacent element A, for example, the index difference of two adjacent elements per row of A is 1, so stride(A,1)=1; the index difference of two adjacent elements per column of A is column number minus 1, where the column number of A is 4, so stride(A,2)=3

```julia> stride(A,2)
3

julia> stride(A,1)
1
```

# Accessing elements in an array

```julia> A = rand(Float64,3,3)
3×3 Array{Float64,2}:
0.934257  0.728261  0.165961
0.55983   0.681753  0.237082
0.922818  0.251498  0.619831

julia> A[1,2]
0.7282612277314509

julia> A[1:3,1] # Access column 1 of A
3-element Array{Float64,1}:
0.9342567217098787
0.5598298924631933
0.9228178819973683

julia> c = [1,3];
julia> A[c,:] #Visit A's No.1,3That's ok
2×3 Array{Float64,2}:
0.934257  0.728261  0.165961
0.922818  0.251498  0.619831

julia> A[:,c] #Visit A's No.1,3column
3×2 Array{Float64,2}:
0.934257  0.165961
0.55983   0.237082
0.922818  0.619831

julia> A[end,1] #end is used in a dimension to represent the last element of that dimension
0.9228178819973683

julia> A[end] #Only one end represents the last element flattened by column
0.6198311712639313
julia> A[end,end]
0.6198311712639313
```

Posted by Nicklas on Fri, 19 Jun 2020 18:08:27 -0700