Announcements


Schedule

  • 2024-09-06 - Introduction. MATLAB rehearsal.
  • 2024-09-13 - Lab 1
  • 2024-09-20 - Lab 2
  • 2024-09-24 - Redovisning lab 1&2
  • 2024-09-26 - Lab 3
  • 2024-10-04 - Lab 4
  • 2024-10-08 - Redovisning lab 3&4
  • 2024-10-11 - Extra redovisning session

Grading

  • To pass the labs you need to complete all labs tasks, present them during the "redovisning" sessions and get them approved.
  • On the "redovisning" sessions we will be asking questions regarding your code: how it works, what it does, etc. You are expected to be able to explain it.
  • Please note that everyone working in a group is expected to be able to answer questions regarding the solutions. It is possible that some people in a group pass the "redovisning" but other people do not.

Course materials


Short syntax overview

Basic syntax
Assign value 1 to variable x x = 1; Documentation
Reference particular element in a vector v at index n v(n) Documentation
Assign new value to an element in a vector v at index n v(n) = new_value Documentation
Reference particular element in a matrix M at row row and column column M(row, column) Documentation
Assign new value to an element in a matrix M at row row and column column M(row, column) = new_value Documentation
Refer to all elements in a row n in a matrix M M(n, :) Documentation
Refer to all elements in a column n in a matrix M M(:, n) Documentation
Number of rows in a matrix M size(M, 1) Documentation
Number of columns in a matrix M size(M, 2) Documentation
Number of elements in a vector v length(v) Documentation
Print value of a variable var disp(var) Documentation
Clear workspace clear; Documentation
Create regularly-spaced vector starting from the number from to the number to with step step v = from:step:to Documentation
Create unit-spaced vector starting from the number from to the number to (the same as from:1:to) v = from:to Documentation
Square root of x sqrt(x) or (x)^0.5 or (x)^(1/2) Documentation
And && Documentation
Or || Documentation
Not ~ Documentation
Determine equality a == b Documentation
Determine inequality a ~= b Documentation
Order of operations in Matlab See documentation Documentation
if, elseif, else
Execute statements if condition is true. Documentation

if condition
    statements
elseif another_condition
    statements
else
    statements
end
for loop
Repeat statement for each element in a vector. At each iteration it puts next element from the vector values to the variable value. Documentation

for value = values
   statements
end
while loop
Repeat statement when condition is true. Documentation

while condition
    statements
end
function
Declare a function with name name, where y1,...,yN are output variables (put your variables instead of ...) and x1,...,xM are input variables. Documentation

function [y1,...,yN] = name(x1,...,xM)

Code examples

Finding the maximum element in a vector

clear;

v = [1, 4, 7, 2, 19];

maximum = v(1);

for i = 1:length(v)
    element = v(i);
    
    if element > maximum
        maximum = element; 
    end
end

disp('The maximum is: ');
disp(maximum);
Print "Hello world!" 5 times

clear;
v = 1:1:5;

for element=v
    disp('Hello world!');
end
Sum of elements in a vector

clear;
v = [1, 4, 61, 4];

sum = 0;
for element=v
    sum = sum + element;
end

disp(sum);
Sum of elements in a vector using indices (the same as previous)

clear;
v = [1, 4, 61, 4];

sum = 0;
for index=1:1:length(v)
    element = v(index);
    sum = sum + element;
end

disp(sum);
Sum of elements in each column of a matrix

clear;
M = [1, 3;
    2, 2;
    4, 7;
    1, 1];

number_of_rows_in_M = size(M, 1);
number_of_cols_in_M = size(M, 2);

sum = zeros(1, number_of_cols_in_M);

indices_rows = 1:1:number_of_rows_in_M;
indices_cols = 1:1:number_of_cols_in_M;

for row_index=indices_rows
    row = M(row_index, :);

    for col_index=indices_cols
        element = row(col_index);
        % element = M(row_index, col_index); % alternative approach
        sum(col_index) = sum(col_index) + element;
    end
end

disp(sum);
Or alternative code:

clear;
M = [1, 3;
    2, 2;
    4, 7;
    1, 1];

number_of_rows_in_M = size(M, 1);
number_of_cols_in_M = size(M, 2);

sum = zeros(1, number_of_cols_in_M);


for row_index=1:1:number_of_rows_in_M
    row = M(row_index, :);

    for col_index=1:1:number_of_cols_in_M
        element = row(col_index);
        % element = M(row_index, col_index); % alternative approach
        sum(col_index) = sum(col_index) + element;
    end
end

disp(sum);
Simple if elseif else example

clear;
a = 1; % try to change
b = 1; % try to change

if a == 0 && b == 0
    disp('if');
elseif b > 0 || a < 0
    disp('elseif');
else
    disp('else');
end
All pairs of vector elements

clear;

numbers = [1, 4, 6, 7, 10];


for v_index = 1:length(numbers)-1
    v = numbers(v_index);
    for w_index = v_index+1:length(numbers)
        w = numbers(w_index);

        pair = [v, w];
        disp(pair);
    end
end
Product of two numbers using function
The function has two input variables: a and b. The result of these two numbers product is stored in the variable called prod, hence it is output of the function. Please note that you cannot run the script with function, since a function needs concrete input to work. You should have only one function in a file.
File product.m:

function prod=product(a, b)
    prod = a*b;
end
After you created a function, you can execute it from any other place by passing input values. File calculate_products.m:

p1 = product(1,2);
disp(p1);
p2 = product(3,5);
disp(p2);

a = 12;
b = 23;
p3 = product(a, b);
disp(p3);
p4 = product(2, b);
disp(p4);