Octave Tutorial GEN0001, Spring 2019
Octave Tutorial GEN0001, Spring 2019
Matlab
Overview
Octave is the "open-source Matlab"
Octave is a great gnuplot wrapper
www.octave.org
www.mathworks.com
This tutorial:
This tutorial applies to Octave *and* Matlab
unless stated otherwise!
Workspace
Command
Window
Command
History
octave:1>
Suppress Output
Add a semicolon:
octave:2> a;
octave:3> sin(phi);
Alternative Example:
octave:2> phi = pi/3;
octave:3> R = [cos(phi) -sin(phi); sin(phi) cos(phi)]
R =
0.50000 -0.86603
0.86603 0.50000
Matrices
Creating a Matrix from Matrices
octave:1> A = [1 1 1; 2 2 2]; B = [33; 33];
Column-wise
octave:2> C = [A B]
C =
1 1 1 33
2 2 2 33
Row-wise:
octave:3> D = [A; [44 44 44]]
D =
1 1 1
2 2 2
44 44 44
Matrices
Indexing
Always "row before column"!
aij = A(i,j) Get an element
r = A(i,:) Get a row
c = A(:,j) Get a column
B = A(i:k,j:l) Get a submatrix
Adding a Row/Column
If the referenced row/colum doesn't exist, it's added.
octave:3> A(4,:) = 4
A =
1 2 3 4 5
2 2 2 2 2
-3 -3 -3 -3 -3
4 4 4 4 4
Matrices
Deleting a Row/Column
Assigning an empty matrix [] deletes the
referenced rows or columns. Examples:
octave:4> A(2,:) = []
A =
1 2 3 4 5
-3 -3 -3 -3 -3
4 4 4 4 4
octave:4> A(:,1:2:5) = []
A =
2 4
2 2
-3 -3
4 4
Matrices
Get Size
nr = size(A,1) Get number of rows of A
nc = size(A,2) Get number of columns of
A
[nr nc] = size(A) Get both (remember
order)
l = length(A) Get whatever is bigger
numel(A) Get number of elements in
A
isempty(A) Check if A is empty matrix
[]
Octave only:
Matrices
Matrix Operations
B = 3*A Multiply by scalar
C = A*B + X - D Add and multiply
B = A' Transpose A
B = inv(A) Invert A
s = v'*Q*v Mix vectors and matrices
d = det(A) Determinant of A
[v lambda] = eig(A) Eigenvalue
decomposition
[U S V] = svd(A) Sing. value
decomposition
Matrices
Vector Operations
With x being a column vector
s = x'*x Inner product, result is a scalar
X = x*x' Outer product, result is a
matrix
e = x*x Gives an error
Examples:
[m n l] = size(A)
A = rand(m,n,l)
m = min(min(min(A)))
aijk = A(i,j,k)
A(:,:,5) = -3
Assessment 1
1. Create the following matrix
2. Find its size
3. Find its transpose matrix A.
4. Find the second row of A and store it in vector B.
5. Add first column in F to B (element-wise addition)