From 7a3cb4e776484750d4ce43f72fe3c62ea64247b5 Mon Sep 17 00:00:00 2001 From: Max Johansson <max.johansson@liu.se> Date: Wed, 29 Mar 2023 15:56:19 +0200 Subject: [PATCH] commit msg --- 1_Introduction/introduction.m | 30 ++++++++++++++++-------------- 2_Indexing/indexing.m | 10 +++++----- 3_functions/functionScript.m | 4 ++-- 4_Debugger/debuggerScript.m | 2 +- 4 files changed, 24 insertions(+), 22 deletions(-) diff --git a/1_Introduction/introduction.m b/1_Introduction/introduction.m index 2b32283..9868a67 100644 --- a/1_Introduction/introduction.m +++ b/1_Introduction/introduction.m @@ -20,7 +20,7 @@ % Press F5 to run entire scripts % Sections -% Type double-percent signs to mark sections +% Type double-percent signs to mark sections: -> %% % Partition code for readability, % run current section with ctrl + enter disp('hello') @@ -44,22 +44,21 @@ z = x + y; % We can also make an empty variable: a = []; -% Workspace: -% Stores temporary data +% Workspace: Stores temporary data % vectors v = [1; 2; 3]; w = [1, 2, 3]; % vectors with linear spacing, both these methods generate row vectors -% use ful when we define grids for dynamic programming +% useful when we define grids for dynamic programming v = 1:0.5:100; % <- from 1 to 100, taking steps of 1/2 w = linspace(1,100,200); % <- from 1 to 100, generating 200 steps % logarithmic spacing: u = logspace(0,2,200); % <- from 10^0 to 10^2, generating 200 steps -% help logspace +% type help to get information about matlab commands: help logspace % transpose v = v'; @@ -86,11 +85,11 @@ title('Plotting vectors') L = legend('1:0.5:100', 'linspace', 'logspace'); L.Location = 'northwest'; -yyaxis right +yyaxis right % <- using two axes in one plot is sometimes useful plot(1./u) -% Variable editor +%% Variable editor % Clean-up: % removing variables: clear @@ -119,7 +118,7 @@ C = [A; B]; % Horizontally D = [A B]; -% Very useful tool, repmat: +% Very useful tool, repmat: Repeats matrices a specified number of times A = eye(2); B = repmat(A, 5, 2); @@ -136,8 +135,8 @@ y = linspace(-10, 10, 10)'; [X,Y] = meshgrid(x,y); % Its use might not be obvious right now, but consider a function -% of one variable f(x). Its convenient to use a vector for x here, so -% that we may evaluate f for all values of x in one go. Similarly if +% of one variable f(x). It's convenient to use a vector for x here, so +% that we may evaluate f for all values of x in one go. Similarly, if % f is a function of two variables x and y, we can evaluate f on the entire domain % at once using X and Y from meshgrid. Example (we will return to functions % and plotting shortly) @@ -185,6 +184,7 @@ max(A) % maximum element of each column (default) max(A,1) % compares each entry to 1 and takes the maximum, does not give maximum of column one max(A, [], 1) % maximum element of each column max(A, [], 2) % maximum element of each row +max(max(A)) % maximum element in the matrix % different functions applied to matrices works similarly % but take nothing for granted, check that matlab does what you @@ -198,7 +198,7 @@ sum(sum(A)) % sum of all elements % Adding matrices is clear: % Adding matrices of wrong sizes will not work -% "Matrix dimensions must agree." - Matlab motto +% "Matrix dimensions must agree." - Common matlab error A = eye(2); B = eye(3); C = A + B; @@ -245,7 +245,7 @@ C = A .* v; % Matrix inverses D = diag([1,2,3]); A = inv(D); % Inverse (usually not recommended) -A = D \ eye(3); % Produces the same result, but superior numerics +A = D \ eye(3); % Produces the same result, but superior numerically %% Special variables @@ -273,7 +273,7 @@ l1 = 1; l2 = 2; l3 = 3; -% check their sum... +% check their sum... L = 6? L = l1 + 12 + 13; @@ -288,7 +288,9 @@ x=3; longName = 5; x = 3; -% ctrl + I is a nice shortcut +% ctrl + I is a nice shortcut to format code: +% Mess up the formatting in the loop below. Then highlight it and press +% ctrl + I for j = 1:length(x) if j < 3 y = 4; diff --git a/2_Indexing/indexing.m b/2_Indexing/indexing.m index 43939a3..cbf65fa 100644 --- a/2_Indexing/indexing.m +++ b/2_Indexing/indexing.m @@ -67,7 +67,7 @@ max(A, [], 2) % maximum element of each row v = linspace(1,10,10); [~,idx] = max(v); -% We can use find to ... find certain elements in a vector or matrix +% We can use find() to find certain elements in a vector or matrix % no extra inputs finds nonzero elements, for v, all elements are nonzero idx = find(v); @@ -86,7 +86,7 @@ v = [1, 2, inf]; idx = isinf(v); % returns a logical vector of inf-locations (not the same as earlier) v(idx); % but we can use it the same way, to find elements that are = inf -% we also have isnan etc. +% we also have isnan, etc. % Another example: any() and all() % will return true if any element of v is true (or non-zero) @@ -94,13 +94,13 @@ any( 0 ) % returns false any([0,1]) % returns true % all() works the other way around, it requires that all elements are true -all(0) % returns falce +all(0) % returns false all([0,1]) % returns false % These are useful to check vectors in if-statements. Its easy to make the % mistake here -% if we compare a vector to a scalar, we check for each element +% Common mistake: if we compare a vector to a scalar, we check for each element v = [0;1]; v >= 1 @@ -118,7 +118,7 @@ if any(v) >= 1 disp('hello world') end -% or equivalently +% or equivalently, v(1) >= 1 OR v(2) >= 1 if v(1) >= 1 || v(2) >= 1 disp('hello world') end diff --git a/3_functions/functionScript.m b/3_functions/functionScript.m index f8c9084..18c8616 100644 --- a/3_functions/functionScript.m +++ b/3_functions/functionScript.m @@ -27,7 +27,7 @@ f(y) % we can define more function handles g = @(x) x + 1; -% unfortunately, this does not work +% How about adding functions? unfortunately, this does not work h = f + g; % but this does @@ -41,7 +41,7 @@ clearvars -except h % and attempt to evaluate h(3) h(3) -% it works. the function h remembers what f and g are, and does not need +% it works. the function h remembers what f and g were, and does not need % to call them. This is not true for functions of the other two kinds % Here is a useful example diff --git a/4_Debugger/debuggerScript.m b/4_Debugger/debuggerScript.m index e1a30dc..4a675c8 100644 --- a/4_Debugger/debuggerScript.m +++ b/4_Debugger/debuggerScript.m @@ -34,7 +34,7 @@ z = z + y; % select more advanced debugging tools, for example conditional breakpoints % % 1. Mark a certain line and press "Breakpoints -> Set condition" -% 2. If we set x > 3 at line 36, the debugger will stop +% 2. If we set x > 3 at line 39, the debugger will stop x = 4; z = x + 3; -- GitLab