Julia Arrays Guide — Array Construction, Indexing, Broadcasting, and Operations
In this tutorial, you will learn about Julia Arrays Guide. We cover key concepts, practical examples, and best practices to help you master this topic.
Julia arrays are N-dimensional containers with 1-based indexing, supporting element-wise operations via broadcasting (.), slicing with ranges, comprehensions for concise construction, and seamless integration with linear algebra functions in Base.
Creating Arrays
# 1D array
v = [1, 2, 3, 4, 5]
# 2D array (column-major)
m = [1 2 3; 4 5 6]
# 2x3 matrix
# Range
r = 1:10 # Range (lazy)
a = collect(1:10) # Materialize
# Comprehension
squares = [x^2 for x in 1:10]
pairs = [(x, y) for x in 1:3, y in 1:3]
# Pre-allocated
zeros(5) # Float64[0,0,0,0,0]
ones(3, 4) # 3x4 matrix of 1.0
rand(2, 3) # 2x3 uniform random
fill("hello", 3) # String["hello","hello","hello"]
Indexing and Slicing
arr = [10, 20, 30, 40, 50]
# 1-based indexing
arr[1] # 10
arr[end] # 50
arr[end-1] # 40
# Slicing
arr[2:4] # [20, 30, 40]
arr[2:2:end] # [20, 40] (step)
arr[[1, 3, 4]] # [10, 30, 40]
# 2D indexing
m = [1 2 3; 4 5 6]
m[1, 2] # 2 (row 1, col 2)
m[2, :] # [4, 5, 6] (row 2)
m[:, 2] # [2, 5] (col 2)
m[1:2, 2:3] # 2x2 submatrix
Broadcasting
# Element-wise operations with dot (.)
x = [1, 2, 3]
x .+ 1 # [2, 3, 4]
x .* 2 # [2, 4, 6]
x .^ 2 # [1, 4, 9]
# Two arrays
y = [4, 5, 6]
x .+ y # [5, 7, 9]
# Function broadcasting
sin.(x) # broadcast sin over array
map(sin, x) # equivalent
# Multiple arrays
[x, y] .+ [10, 20] # [11, 25], [14, 26]
Array Manipulation
v = [3, 1, 4, 1, 5]
# Add elements
push!(v, 9) # [3, 1, 4, 1, 5, 9]
append!(v, [2]) # [3, 1, 4, 1, 5, 9, 2]
pushfirst!(v, 0) # insert at beginning
# Remove elements
pop!(v) # remove last
popfirst!(v) # remove first
deleteat!(v, 3) # remove at index
# Reorder
sort(v) # sorted copy
sort!(v) # in-place sort
reverse(v) # reverse
unique(v) # unique elements
Common Mistakes
1. Using 0-based indexing
Julia uses 1-based indexing. arr[0] throws a BoundsError. First element is arr[1].
2. Forgetting broadcast dot
sin(arr) works if sin is defined for arrays, but sin.(arr) is element-wise. The dot is required for most functions.
3. Column-major confusion
Julia stores matrices in column-major order. Iterating column-first is faster: for j in 1:size(m,2), i in 1:size(m,1).
Practice Questions
1. How do you create a 2x3 matrix of zeros?
zeros(2, 3) creates a 2x3 Float64 matrix. Use zeros(Int, 2, 3) for integer type.
2. What does the dot operator do in Julia?
Broadcasts a function or operator over array elements: [1,2,3] .+ 1 gives [2,3,4].
3. How do you access the last element of an array?
arr[end] or arr[length(arr)] or arr[end].
FAQ
{{< faq question="Are arrays 0-indexed or 1-indexed?" >}} 1-indexed. Julia uses 1-based indexing consistently across all collections. {{< /faq >}}
{{< faq question="What is the difference between push! and append!?" >}} push! adds a single element. append! adds all elements from another collection. Both modify in-place. {{< /faq >}}
{{< faq question="How do I create an empty array of a specific type?" >}}
Int[] or Float64[] or Vector{Float64}() for 1D. Matrix{Float64}() for 2D.
{{< /faq >}}
What's Next
Now learn about linear algebra in Julia.
| Topic | Description | Link |
|---|---|---|
| Linear Algebra | Matrices and linear algebra | {{< ref "06-linear-algebra" >}} |
| String Handling | String manipulation | {{< ref "07-strings" >}} |
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro