Julia 101
Lesson 3.1: Manipulating Matrices
In Julia, we can define a matrix by typing in a list of numbers enclosed in square brackets.
A = [0 1 2 3 4 5 6 7 8 9]
Above statement returns a 1 by 10 Matrix.
A semicolon is used to add a new row in a matrix. The statement
A = [0 1 2 3 4; 5 6 7 8 9]
returns a 2 by 5 Matrix.
Matrix can also be define by writing each row on a seperate line. The statement
A = [0 1 2 3 4;
5 6 7 8 9]
or
A = [0 1 2 3 4
5 6 7 8 9]
still returns a 2 by 5 Matrix.
Julia also allows us to define a matrix in terms of another matrix that has already been defined. For example, statements
A = [0 1 2 3]
B = [A 4 5 6]
return B = 0 1 2 3 4 5 6
We can change or add additional elements to a matrix, by using an index number to specify a particular element. Like MATLAB, in Julia, indexing starts at 1, not 0, therefore command B[1]
returns 0
and commandB[7]
returns 6.
The commandB[1] = 10
changes the first value in matrix B
from 0
to 10.
Similarly, the commandB[4] = 11
changes the fourth value in matrix B
from 3
to 11.
After these commands, B
is of the form:
B = 10 1 2 11 4 5 6
That’s all for now. In the next lesson, we will discuss how the colon:
operator is used to define and modify matrices.