TNK055 Matlab part

Announcements

  • Welcome to the class! A word of caution: Information on this site is not final until the class starts.

Course Information

  • The course consists of 4 labs. Each lab has a short tutorial and several tasks.
  • After doing every lab, students are expected to send the solutions (Matlab files) to anastasia.lemetti@liu.se.
  • All labs are graded Pass or Fail.
  • The final exam is based on the last lab (Lab 4).

Deadlines

The recommended deadline for all labs is 2022-10-14.

The strict deadline for all labs is the date of the exam.

Labs

Grading

Exam, conducted online via Zoom. Exam is graded on the scale Fail/3/4/5.

Exam date and time: 25th of October, 13.15-15.00.


Additional materials


Short syntax overview

Basic syntax
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)

Useful functions

find
Find indices and values of nonzero elements Documentation

a = [-1, 6, 0, 4, 6];
indices1 = find(a > 1); % indices1 == [2, 4, 5]
indices2 = find(a == 6); % indices2 == [2, 5]

To select values from the vector use


a = [-1, 6, 0, 4, 6];
indices = find(a > 1); % indices1 == [2, 4, 5]
values = a(indices); % values == [6, 4, 6]

To find string in a cell array of strings use contains():


A = {'fish', 'dog', 'cat', 'horse'};
index = find(contains(A, 'dog')); % index == 2

Or ismember() (returns an array with 1 where the data in A is found in B):


A = {'dog','cat','fish','horse'};
B = {'dog','mouse','fish'};
res = ismember(A, B); % res == [true, false, true, false] or [1, 0, 1, 0]
azimuth
Azimuth between points Documentation

segment_start_point = [59.6569444500000	17.9522222166667]; % lat and lon
segment_end_point = [59.6519444500000	17.9186111166667]; % lat and lon

result = azimuth(segment_start_point(1), segment_start_point(2), segment_end_point(1), segment_end_point(2)); % result == 253.6074
unique
Unique values in array Documentation

a = [3, 6, 4, 3, 4, 5];
result = unique(a); % result == [3, 4, 5, 6]

Unique also works with matrices:


a = [3, 6;
4, 3;
3, 6];
result = unique(a, 'rows'); % result == [3, 6; 4, 3]
histogram
Histogram plot Documentation

a = [3, 6, 4, 3, 4, 5];
histogram(a); % try it!

Number of bins can be passed in the second argument of the function:


a = [3, 6, 4, 3, 4, 5];
histogram(a, 2); % try it!

Also, bins can be defined manually:


a = [3, 6, 4, 3, 4, 5];
histogram(a, [0, 3, 5, 6]); % try it!

Bins can be named:


a = [4, 3, 5];
histogram('Categories', {'First', 'Second', 'Third'}, 'BinCounts', a); % try it!
intersect
Set intersection of two arrays Documentation

A = [7 1 7 7 4];
B = [7 0 4 4 0];
C = intersect(A,B); % C == [4, 7]

Also intersect works with matrices


A = [2 2 2; 0 0 1; 1 2 3; 1 1 1];
B = [1 2 3; 2 2 2; 2 2 0];
C = intersect(A, B, 'rows'); % C == [1, 2, 3; 2, 2, 2]
strsplit
Split string at specified delimeter Documentation

res = strsplit('ABCD_DCBA', '_'); % res == {'ABCD', 'DCBA'}
distance
Distance between points on sphere or ellipsoid Documentation

arc_length = distance(37, -76 , 37, -9);
deg2nm
Convert spherical distance from degrees to nautical miles Documentation

arc_length_in_nm = deg2nm(distance(37, -76 , 37, -9));