We will rely on the Octave Kernel for Jupyter Notebook for running code and visualization for this tutorial.
You can use Octave from the terminal, with a jupyter notebook or via the OctaveGUI application. Octave provides you with a Read-Eval-Print-Loop - short REPL - where you can run your commands.
help log
realScalar = 5
complexScalar = 5 + 2i;
When appending a semicolon ;
the output will be omitted. Typing the variable name will output its value:
complexScalar
matrices
rowVector = [1 2 3 4 5]
rowVector = 1:5
colVector = [1; 2; 3; 4; 5]
colVector = (1:5)'
# compare to
colVector = 1:5'
The '
in this example stands for transpose. Octave will transpose the respective vector. Be careful how it's interpreted by Octave though.
matrix = [1 2 3; 4 5 6; 7 8 9]
You can also work with complex matrices (won't be covered in this course, hence omitted here).
index expressions
a = [1:2; 3:4]
a(2) # result is a scalar
a(1:2) # result is a row vector
a([1; 2]) # result is a column vector
a(:) # result is a column vector
a(:)' # result is a row vector
a(1, [1, 2]) # row 1, columns 1 and 2
a(1, 1:2) # row 1, columns in range 1-2
a(1, :) # row 1, all columns
a = [1, 2, 3, 4];
a(1:end/2) # first half of a => [1, 2]
a(end + 1) = 5; # append element
a(end) = []; # delete element
a(1:2:end) # odd elements of a => [1, 3]
a(2:2:end) # even elements of a => [2, 4]
a(end:-1:1) # reversal of a => [4, 3, 2 , 1]
special utility matrices
eye(3)
ones(3)
zeros(3)
rand(3)
for more see here
character strings
You can use both single ''
and double parentheses ""
for defining strings. String will probably only important when setting the labels for your figures.
string_1 = 'abc'
string_2 = "abc"
data structure type
strct.scalar = 1;
strct.matrix = [1, 2; 3, 4];
strct.string = "string";
strct
cell arrays
c = {"a string", rand(2, 2)};
c{1}
c{2}
c{1:2}
a = (1:4)'
Dot-Product $a^{T}a$
a' * a
Outer Product - $aa^{T}$
a * a'
A = [1 2; 3 4]
Matrix Multiplication - $AA$
A * A
Dimensions must match!
B = [1 2 3; 4 5 6]
B * A
Matrix Vector Multiplication - $Xb$
X = [1:3; 4:6; 7:9]
b = [1; 1; 1]
X*b
Octave also features functions from Linear Algebra for LU or QR decomposition, linear systems solvers, etc.
You can find more about that here
graphics_toolkit("gnuplot")
run this command if the plots are black
%plot --format svg
x = rand(5, 1)
1-dimensional
plot(x)
x = rand(100, 1);
y = rand(100, 1);
2-dimensional
plot(x, y, '.')
x = linspace(0, 2*pi, 50);
y = sin(x);
plot(x, y, '+1;sin(x);');
x = linspace(0, 2*pi, 100);
y = sin(x);
y2 = cos(x);
plot(x, y, '+1;sin(x);', "markersize", 10, x, y2, ";cos(x);", "markersize", 5, "marker", '*');
plot(x, y, '+1;sin(x);', "markersize", 10);
hold on # <--- will keep your current plot
plot(x, y2, ";cos(x);", "markersize", 5, "marker", '*')
Nice! Unfortunately we are still missing labels. A different colorscheme and lines instead of the the points would also be nice. So back to the plotting board:
plot(
x, y, "-;sin(x);", "color", [27,158,119] ./255, "linewidth", 5,
x, y2, "--;cos(x);", "color", [117,112,179] ./255, "linewidth", 5);
set(gca, 'xtick', [0, pi, 2*pi])
#set(gca, 'xticklabel', {'0', '\pi', '2 \pi'}) # <--- should work, maybe a bug
set(gca, 'interpreter', 'tex')
xlabel('{\fontsize{30} \pi}')
ylabel('{\fontsize{30} value}')
title('{\fontsize{50} Sinus and Cosinus}')
plot matrix columns as multiple lines
X = randn(10, 4)
plot(X)
if - Statement
a = 5
if a > 3
disp([num2str(a),' is larger than 3'])
elseif a < 3
disp([num2str(a),' is smaller than 3'])
else
disp([num2str(a),' is equal to 3'])
end
switch - Statement
animal = 'cow'
switch animal
case 'dog'
disp('Woof')
case 'cat'
disp('Meow')
case 'bird'
disp('Tweep')
otherwise
disp(['Error: no sound available for animal ', animal])
end
while - Statement
x = 2
while x < 16
x = x*2
end
break - statement
for i = 1:5
i
end
Incrementing Operators (Octave only!)
i = 1
i++ # <--- what's happening here?
++i
A = zeros(2,2)
A++ # <--- what's happening here?
A--
A
Comparison operators
All of comparison operators return a value of 1 if the comparison is true, or 0 if it is false.
1 != 3
matrix-to-matrix
[1 2; 3 4] == [1 4; 2 3]
matrix to vector (by row and by column)
[1 2; 1 2] == [1 2]
[1 2; 1 2] == [1 2]'
matrix to scalar
[1 2; 3 4] == 1
Comparison Operators
v = ones(1, 10)
any - returns 1 if any element of vector v is non-zero
any(v)
all - returns 1 if all elements of vector are non-zero
all(v)
for matrices, any and all return a row vector with elements corresponding to the columns of the matrix
A = ones(4)
any(any(A))
all(all(A))
Relational Operators
x = 1
y = 2
x < y #true if x less than y
x <= y #true if x less thon or equal y
x == y #true if x equal to y
x >= y #true if x greater thon or equal y
x > y #true if X greater than y
x ~= y #true if x not equal to y
x ~= y #true if x not equal to y (Octave only)
Boolean Expressions
B1 = true
B2 = false
B1 & B2 #element-wise logical and
B1 | B2 #element-wise logical or
~B1 #element-wise logical not
!B1 #element-wise logical not (Octave only)
Function definition in Octave
function name
body
end
It's good practice to define one function per file (m-file or .m-file)
function parameters Functions usually have one or more parameters and might return multiple results
function [ret-vars] = name (arg-list)
body
end
function b = gemv (A, v)
b = A*v;
end
gemv(ones(4), (1:4)')
function [_min _max] = calc_extremas(data)
_min = min(data);
_max = max(data);
end
[mi, ma] = calc_extremas(rand(10,1))