%% SL 4
N1 = (-5:5:5)';

N1(1:3)
N1([1 2 3])
N1(1:2)
N1([1 3])
N1([1 3].')
N1([1 3]).'
N1([1; 3])
N1([1 3],1)

N2 = [1:5;2:2:10;primes(11)];
N2(1, 3)
N2(3, 1)
N2(1, end)
N2(end, end)
N2(1, :)
N2(1, :).'
N2(:, 2)
N2(:, 3:end)

N3 = (1:4)'*(11:14);
N3(2:3, [1 1 1]) % like repmat
N3(2:3, ones(1,3))
N3(2:3, ones(3,1))
N3([N2(2,1:2)/2 4], [2 3])
N3([1 end], [1:4 1:2:end])
N3(:, :, 2) = magic(4)
N3([1 3], 3:4, 3) = ...
[1/2 -1/2; pi*ones(1, 2)]

%% SL 5
N1(end:-1:1)
N2(:, 2:2:end)
N3(1:2:end, :)
N2(2, [2 4 5])

A = reshape(1:12, 3, 4).'
A = zeros(3, 4), A(:) = 1:12, A = A.'

%% SL 6
clear; clc;
x = 1:20;
S = cumsum(x);
L = cumsum(x(2:2:end));
L(end)

%% SL 7
A([1, 1], [end, end])
A({[1, 1], [1, end], [end, 1], [end, end]})
A([1, end], [1, end])
A(1:end, 1:end)

%% SL 10
A = magic(6);
% A[1, end] = 0
% A(:, 1, end) = []
% A(:, [1:end]) = []
% A(:, [1 end]) = []

clc, clear
A = magic(6);
B = 2*magic(6);
A([2:3 5], :) = B([1 1 1], :);

B = magic(7);
A([2:3 5], :) = B(ones(1, 3), 1:6)

B = magic(5);
A([2:3 5], 1:5) = B(ones(1, 3), :)

%% SL 11
M(:, :, 1) = eye(3);
M(:, :, 2) = ones(3);
M(:, :, 3) = diag([2 3 5]);
% or
M = cat(3, eye(3), ones(3), diag([2 3 5]));

%% SL 19
G = [pi/2 pi 3/2*pi 2*pi]
G > pi
G <= pi
G ~= pi
M(1:2, 1:2, 1) = NaN;
H = G.'; % then analogously

V = [-pi pi 1 0]
U = [1 1 1 1]
V >= U

%% SL 20
2 < 1 ~= 1 % ???

r = 1/2;
0 < r < 1 % ???

A = 1
r = 1/2;
0 < r < 1 % ???

%% SL 23
A = pi/2:pi/2:2*pi;
A == pi | A == 2*pi
A > pi/2 & A ~= 2*pi
A = [A A(A > pi/2 & A ~= 2*pi)]

%% Sl 24
K = 1:3:20;
L = K(K > 10 & K < 16)

%% SL 25
M = magic(3);
all(M > 2)
any(M >= 8, 2)
all(all(M > 0))
all(M(:) > 0)

%% SL 27
~(~[1 2 0 -2 0])

%% SL 29
clear; clc; % clear Workspace
N = 5; V = 40; T = 1; % period, sampling
mu = 0; sigma = 1; % normal distribution
C = sqrt(2*pi); om0 = pi;
n = mu + sigma*randn(1, N*V); % noise
t = linspace(0, N*T, N*V); % time
s_t = C*sin(2*om0*t) + n; % signal
plot(t, s_t);

%% SL 30
smin = -9/10;
smax = pi/2;
sp_t= max(s_t, smin);
sp_t = min(sp_t, smax);

close all;
plot(t, s_t); hold on;
stem(t, sp_t, 'r');

%% S 31
sp2_t = s_t;
sp2_t(sp2_t > pi/2) = pi/2;
sp2_t(sp2_t < -9/10) = -9/10;
stem(s_t);

close all;
plot(t, s_t); hold on;
stem(t, sp2_t, 'r');

%% SL 32
clear; clc;
r = 0.1:0.01:0.2; % interest on a loan is vectorized
A = 1e3;
n = 12;
k = 15;
P = r*A.*(1 + r/n).^(n*k) ./ (n*((1 + r/n).^(n*k) - 1));
disp(P.'); % monthly instalments
disp(P.'*n*k - A); % overpayment

%% SL 34
clear; clc; close all;
r = 0.1:0.01:0.2;
A = 1e3;
n = 12;
k = (1:15).';
% using Compatible Array Size
P = r*A.*(1 + r/n).^(n*k) ./ ...
(n.*((1 + r/n).^(n*k) - 1));
surf(r, k, P)
xlabel('r (-)');
ylabel('k (years)');
zlabel('P (-)');

%% SL 36
X = zeros(1, 45);
X(6:15) = 1;
X(16:25) = 2;
X(25:end) = 2:-2/20:0;
stem(X, 'r');

% or
X = [zeros(1, 5) ones(1, 10)
2*ones(1, 9) 2:-2/20:0];
stem(X, 'r');

%% SL 37
A = [1 1 2; 2 3 5];
all(all(A > 0)) && all(mod(A(1, :), 1) == 0)