# Creating a simple vector (list) of numbers
= [95, 87, 91, 78, 88] grades
5-element Vector{Int64}:
95
87
91
78
88
Applied Optimization with Julia
This interactive Julia script introduces the basics of vectors, matrices, and tuples.
Understanding these data structures and how to manipulate them is crucial in Julia. Follow the instructions, write your code in the designated code blocks, and execute the corresponding code cell.
If a cell is marked with YOUR CODE BELOW
, you are expected to write your code in that cell.
Vectors in Julia are one-dimensional arrays used to store sequences of elements. They’re powerful for numerical operations and data handling. A vector is the simplest way to store a list of items. Think of it as a row of boxes, where each box can hold a number or other type of data. Create vectors with square brackets, separate elements with commas.
# Creating a simple vector (list) of numbers
= [95, 87, 91, 78, 88] grades
5-element Vector{Int64}:
95
87
91
78
88
# Creating a vector of names
= ["Mike", "Yola", "Elio"] students
3-element Vector{String}:
"Mike"
"Yola"
"Elio"
Vectors are mutable, and you can:
push!(grades, 82)
pop!(grades)
popfirst!(grades)
grades[1]
gives you the first gradegrades[1:3]
gives you the first three gradesIn Julia, we start counting positions from 1, not 0. So grades[1]
gives you the first grade!
Use ‘?’ in the REPL for function details.
Create a vector ‘fib’ with the first five Fibonacci numbers: 1, 1, 2, 3, 5
.
# YOUR CODE BELOW
# Test your answer
@assert fib == [1, 1, 2, 3, 5]
println("The 'fib' vector: ", fib)
Append the number 8 to the fib
vector.
# YOUR CODE BELOW
# Test your answer
@assert fib == [1, 1, 2, 3, 5, 8]
println("The 'fib' vector after appending 8: ", fib)
Remove the first element of the fib
vector.
# YOUR CODE BELOW
# Test your answer
@assert fib == [1, 2, 3, 5, 8]
println("The 'fib' vector after removing the first element: ", fib)
Access and save the first three elements of fib
, to first_three_elements
.
# YOUR CODE BELOW
# Test your answer
@assert first_three_elements == fib[1:3]
println("The first three elements of the 'fib' vector: ", first_three_elements)
A matrix in Julia is a 2D array, great for linear algebra and data representation. Create matrices with square brackets, separate elements with spaces, rows with semicolons. Access elements with square brackets (e.g., matrix[2,2]
). You can add or substract matrices of the same dimensions element-wise if you add or substract them.
For example:
= [2 2; 3 3] + [1 2; 3 4]
matrix1 = [2 2; 3 3] - [1 2; 3 4]
matrix2 println("matrix1 is $matrix1.")
println("matrix2 is $matrix2.")
matrix1 is [3 4; 6 7].
matrix2 is [1 0; 0 -1].
If you want to perform multiplications, things are a bit different, as matrix multiplications are not performed element-wise!
= [2 2; 3 3] * [1 2; 3 4] # Not element-wise
matrix3 println("matrix3 is $matrix3.")
matrix3 is [8 12; 12 18].
But we can change this by using broadcasting!
Julia’s broadcasting feature allows you to apply functions element-wise to arrays of different sizes. This is denoted by adding a dot (.) before the operator or function. It is extremely useful for performing operations on arrays without the need to loop through each element.
= [1 2; 3 4]
matrix4 = matrix4 .+ 1
matrix4 println("matrix4 is $matrix4.")
matrix4 is [2 3; 4 5].
We can also use this to perform element-wise multiplications, as shown below:
= [2 2; 3 3] .* [1 2; 3 4] # Element-wise
matrix5 println("matrix5 is $matrix5.")
matrix5 is [2 4; 9 12].
Create a 2x3 matrix my_matrix
with the values: 1 2 3; 4 5 6
.
# YOUR CODE BELOW
# Test your answer
@assert my_matrix == [1 2 3; 4 5 6]
println("The 'my_matrix':\n", my_matrix)
Change the 3rd column of the 2nd row to 17
by accessing and changing the element.
# YOUR CODE BELOW
# Test your answer
@assert my_matrix == [1 2 3; 4 5 17]
println("The 'my_matrix' after modification:\n", my_matrix)
Perform matrix addition with my_matrix
and another_matrix
. Call the resulting matrix added_matrices
.
= [10 20 30; 40 50 60]
another_matrix # YOUR CODE BELOW
# Test your answer
@assert added_matrices == [11 22 33; 44 55 77]
println("Result of adding 'my_matrix' and 'another_matrix':\n", added_matrices)
Add 10
to each element in added_matrices
.
# YOUR CODE BELOW
# Test your answer
@assert added_matrices == [21 32 43; 54 65 87]
println("Result of adding 10 to each element in 'added_matrices':\n", added_matrices)
A tuple is like a sealed package - once you create it, you can’t change what’s inside. That’s why we call it “immutable”. It’s perfect for grouping related items that shouldn’t change. For example:
# A person's basic info that won't change
= ("Elio Smith", 18, "Hamburg")
person
# RGB color values
= (255, 0, 0) red
Think of tuples as permanent labels - once you write them, they can’t be changed. This is useful when you want to make sure data stays exactly as you set it.
Create a tuple my_tuple
with three elements: 4.0
, your matrix added_matrices
, and "Hi there!"
.
# YOUR CODE BELOW
# Test your answer
@assert my_tuple == (4.0, added_matrices, "Hi there!")
println("The 'my_tuple': ", my_tuple)
Access the second element of my_tuple
, store it in second_element
.
# YOUR CODE BELOW
# Test your answer
@assert second_element == [21 32 43; 54 65 87]
println("The second element of 'my_tuple':\n", second_element)
Well done! You’ve completed the tutorial on Vectors, Matrices, and Tuples. You’ve learned to create, manipulate, and interact with these fundamental data structures. Experiment with the code, try different operations, and see how Julia behaves. Continue to the next file to learn more.
You will likely find solutions to most exercises online. However, I strongly encourage you to work on these exercises independently without searching explicitly for the exact answers to the exercises. Understanding someone else’s solution is very different from developing your own. Use the lecture notes and try to solve the exercises on your own. This approach will significantly enhance your learning and problem-solving skills.
Remember, the goal is not just to complete the exercises, but to understand the concepts and improve your programming abilities. If you encounter difficulties, review the lecture materials, experiment with different approaches, and don’t hesitate to ask for clarification during class discussions.
Later, you will find the solutions to these exercises online in the associated GitHub repository, but we will also quickly go over them in next week’s tutorial. To access the solutions, click on the Github button on the lower right and search for the folder with today’s lecture and tutorial. Alternatively, you can ask ChatGPT or Claude to explain them to you. But please remember, the goal is not just to complete the exercises, but to understand the concepts and improve your programming abilities.