= [1, 3, 5, 7] x
4-element Vector{Int64}:
1
3
5
7
Julia includes builtin N-dimensional arrays.
Build an Array using square brackets:
= [1, 3, 5, 7] x
4-element Vector{Int64}:
1
3
5
7
There are multiple ways to initialize an Array. The main constructor Array
can be used with the syntax Array{T}(undef, dims)
for T of a given type. For example:
= Array{Float64}(undef, 10, 4) af
10×4 Matrix{Float64}:
0.0 5.0e-324 0.0 0.0
5.0e-324 0.0 0.0 0.0
5.0e-324 0.0 0.0 0.0
0.0 5.0e-324 0.0 0.0
0.0 5.0e-324 0.0 0.0
5.0e-324 5.0e-324 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
5.0e-324 0.0 0.0 0.0
= Array{Int64}(undef, 10, 4) ai
10×4 Matrix{Int64}:
4500111984 4501967728 5225787808 5293716816
0 0 0 1
5225779120 4501086640 5293716816 5293716816
0 1 1 1
4500112000 5153490560 5293716816 5293716816
0 0 1 1
5153520176 5208729952 5293716816 5293716816
0 0 1 1
5208757600 4522071440 5293716816 5153520176
0 0 1 0
= ["a", "b", "c", "d", "e"] as
5-element Vector{String}:
"a"
"b"
"c"
"d"
"e"
#You can define the number of dimensions using Array{Float64, 2}(undef, 10, 4)
, but it is inferred from the length of integers after undef
= [1, 2, 3, 4, 5] a
5-element Vector{Int64}:
1
2
3
4
5
Create array with defined type
= Array{Float64}([1, 2, 3, 4, 5]) a
5-element Vector{Float64}:
1.0
2.0
3.0
4.0
5.0
Create array using a range and collect()
:
= 11:15 b
11:15
typeof(b)
UnitRange{Int64}
= collect(11:15) b
5-element Vector{Int64}:
11
12
13
14
15
fill(42.0, 3)
3-element Vector{Float64}:
42.0
42.0
42.0
repeat([1, 2, 3], 2)
6-element Vector{Int64}:
1
2
3
1
2
3
ones(3)
3-element Vector{Float64}:
1.0
1.0
1.0
zeros(3)
3-element Vector{Float64}:
0.0
0.0
0.0
trues(3)
3-element BitVector:
1
1
1
falses(3)
3-element BitVector:
0
0
0
Use ;
to separate rows:
= [1 2 3; 4 5 6] d
2×3 Matrix{Int64}:
1 2 3
4 5 6
= collect(1:12) x
12-element Vector{Int64}:
1
2
3
4
5
6
7
8
9
10
11
12
= reshape(x, 4, 3) xm
4×3 Matrix{Int64}:
1 5 9
2 6 10
3 7 11
4 8 12
To fill row-wise, we can use permutedims()
(and switch n rows <-> ncols)
permutedims(reshape(x, 3, 4))
4×3 Matrix{Int64}:
1 2 3
4 5 6
7 8 9
10 11 12
You can use :
if you hate multiplication / division:
reshape(x, 4, :)
4×3 Matrix{Int64}:
1 5 9
2 6 10
3 7 11
4 8 12
= reshape(x, 3, 2, 2) xt
3×2×2 Array{Int64, 3}:
[:, :, 1] =
1 4
2 5
3 6
[:, :, 2] =
7 10
8 11
9 12
ones()
, zeros()
, trues()
, falses()
can all construct ND Arrays:
ones(2, 3, 2)
2×3×2 Array{Float64, 3}:
[:, :, 1] =
1.0 1.0 1.0
1.0 1.0 1.0
[:, :, 2] =
1.0 1.0 1.0
1.0 1.0 1.0
1-D Array of 1-D Arrays:
= ["a", "b", "c", "d", "e"] c
5-element Vector{String}:
"a"
"b"
"c"
"d"
"e"
= [a, b, c] arr
3-element Vector{Vector}:
[1.0, 2.0, 3.0, 4.0, 5.0]
[11, 12, 13, 14, 15]
["a", "b", "c", "d", "e"]
Horizontal concatenation: 2-D Array
= hcat(a, b, c) arr
5×3 Matrix{Any}:
1.0 11 "a"
2.0 12 "b"
3.0 13 "c"
4.0 14 "d"
5.0 15 "e"
Vertical concatenation: Combine 1-D Arrays into a a longer 1-D Array
= [a; b] v
10-element Vector{Float64}:
1.0
2.0
3.0
4.0
5.0
11.0
12.0
13.0
14.0
15.0
or using vcat
= vcat(a, b) v
10-element Vector{Float64}:
1.0
2.0
3.0
4.0
5.0
11.0
12.0
13.0
14.0
15.0
varinfo()
name | size | summary |
BUFFER_SIZE | 8 bytes | Int64 |
Base | Module | |
Core | Module | |
MSG_BOUNDARY | 50 bytes | 10-element Vector{UInt8} |
Main | Module | |
MsgID | 124 bytes | DataType |
MsgType | 7 bytes | @NamedTuple{from_host_call_with_response::UInt8, from_host_call_without_response::UInt8, from_host_fake_interrupt::UInt8, from_worker_call_result::UInt8, from_worker_call_failure::UInt8, special_serialization_failure::UInt8, special_worker_terminated::UInt8} |
Notebook | 1.760 KiB | Module |
_buffer_writes | 0 bytes | _buffer_writes (generic function with 1 method) |
_channel_cache | 456 bytes | Dict{UInt64, AbstractChannel} with 0 entries |
_discard_until_boundary | 0 bytes | _discard_until_boundary (generic function with 1 method) |
_serialize_msg | 0 bytes | _serialize_msg (generic function with 1 method) |
format_error | 0 bytes | format_error (generic function with 1 method) |
handle | 0 bytes | handle (generic function with 3 methods) |
interrupt | 0 bytes | interrupt (generic function with 2 methods) |
main | 0 bytes | main (generic function with 1 method) |
refresh! | 0 bytes | refresh! (generic function with 1 method) |
render | 0 bytes | render (generic function with 1 method) |
serve | 0 bytes | serve (generic function with 1 method) |
= [1, 3, 5, 9];
a = [2, 3, 4, 9, 11]; b
Is 3
in b
?
in(b)(3)
true
For each element of a
, check if it exists in b
in(b).(a)
4-element BitVector:
0
1
0
1
in(a).(b)
5-element BitVector:
0
1
0
1
0
Let’s look at logical and integer indexing.
Indexing in Julia is 1-based.
= collect(15:24) x
10-element Vector{Int64}:
15
16
17
18
19
20
21
22
23
24
Get the 5th element of a vector (integer index):
5] x[
19
Get elements 6 through 9 of the same vector (integer index):
6:9] x[
4-element Vector{Int64}:
20
21
22
23
Select elements with value greater than 19 (logical index):
.> 19] x[x
5-element Vector{Int64}:
20
21
22
23
24
= collect(21:30) x
10-element Vector{Int64}:
21
22
23
24
25
26
27
28
29
30
Create a logical index using two inequalities.
Notice how you need to use parentheses for the elementwise .&
to work in this case:
= (x .> 23) .& (x .< 28) idl
10-element BitVector:
0
0
0
1
1
1
1
0
0
0
findall()
= findall(idl) idi
4-element Vector{Int64}:
4
5
6
7
Initialize a BitArray:
= BitArray(undef, length(x)) idl2
10-element BitVector:
0
0
0
0
0
0
0
0
0
0
or using falses()
:
= falses(length(x)) idl2
10-element BitVector:
0
0
0
0
0
0
0
0
0
0
Use the integer index to replace the corresponding elements:
.= true idl2[idi]
4-element view(::BitVector, [4, 5, 6, 7]) with eltype Bool:
1
1
1
1
idl2
10-element BitVector:
0
0
0
1
1
1
1
0
0
0
Negate a logical index with !
/ .!
x[.!idl]
6-element Vector{Int64}:
21
22
23
28
29
30
x[idi]
4-element Vector{Int64}:
24
25
26
27