N1 = (-5:5:5)';   N2 = [1:5;2:2:10;primes(11)];   N3 = (1:4)'*(11:14);

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

N2(1, 3)
N2(3, 1)
N2(1, end)
N2(end, end)
N2(1, :)
N2(1, :).'
N2(:, 2)
N2(:, 3:end)

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)]

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.'

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

A([1, 1], [end, end])
A({[1, 1], [1, end], [end, 1], [end, end]})
A([1, end], [1, end])     
A(1:end, 1:end)                    

A[1, end]     = 0
A(:, 1, end)  = [] 
A(:, [1:end]) = [] 
A(:, [1 end]) = [] 
A([2:3 5], :) = B([1 1 1], :)
A([2:3 5], :) = B(ones(1, 3), 1:6)
A([2:3 5], 1:5) = B(ones(1, 3), :)

M(:, :, 1) = eye(3);
M(:, :, 2) = ones(3);
M(:, :, 3) = diag([2 3 5]);
M = cat(3, eye(3), ones(3), diag([2 3 5]));
M(1:2, 1:2, 1) = NaN;

A = zeros(4);
A(:) = (1:16)
row = [2, 4, 1, 2];
col = [1, 2, 2, 4];
% 1. solution
ind = sub2ind(size(A), row, col)
% 2. (our) solution
ind = (col-1)*size(A, 2)+row

A = magic(4);
B = zeros(size(A));
[val, col] = max(A, [], 2);
B(sub2ind(size(A), 1:4, col.')) = val;

G = [pi/2 pi 3/2*pi 2*pi]
G > pi
G <= pi
G ~= pi
H = G.'; % then analogously
V = [-pi pi 1 0]
U = [1 1 1 1]
V >= U

2 < 1 ~= 1 % ???
r = 1/2;
0 < r < 1 % ???
(1 > A) <= true

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)]

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

M = magic(3);
all(M > 2)
any(M >= 8, 2)
all(all(M > 0))
all(M(:) > 0)
all(M > 0, 'all')
~(~[1 2 0 -2 0])
a = 1;
b = 0;
if b ~= 0 && a/b > 3
    a/b
end

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);

smin = -9/10; smax = pi/2;
sp_t_min = max(s_t, smin);
sp_t     = min(sp_t_min, smax);
close all; 
plot(t, s_t); hold on;
stem(t, sp_t, 'r');

N = 5; V = 40;
t = linspace(0, N, N*V);
s_t = randn(1, N*V) + sqrt(2*pi)*sin(2*pi*t);
s_t(s_t > pi/2)  = pi/2;
s_t(s_t < -9/10) = -9/10;
stem(s_t);

%% script loanRepayment.m
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

%% script loanRepaymentVectorized.m
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 (-)');

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

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

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

b ~= 0 && a/b > 3


