clear; close; clc;


% Row and column vectors
r = [1 2 3 4];        % 1x4 row vector
c = [1; 2; 3; 4];     % 4x1 column vector

% Matrices (rows separated by semicolons)
A = [1 2; 3 4];       % 2x2 matrix
B = [5 6; 7 8];       % 2x2 matrix

% Common constructors
z = zeros(3,2);       % 3x2 of zeros
o = ones(2,3);        % 2x3 of ones
I = eye(3);           % 3x3 identity
D = diag([10 20 30]); % diagonal matrix

clear; close; clc;

% Colon operator: start:step:stop
x = 1:5;          % [1 2 3 4 5]
y = 0:0.5:2;      % [0 0.5 1.0 1.5 2.0]

% linspace(start, stop, nPoints)
g = linspace(0, 1, 6);   % 6 points from 0 to 1

% Quick grids (for later plotting/surfaces)
% [X,Y] = meshgrid(x, y); % not needed now, just a teaser

clear; close; clc;

A = [10 20 30; 40 50 60; 70 80 90] % 3x3

A(2,3)       % element at row 2, col 3 -> 60
A(1,:)      % entire row 1 -> [10 20 30]
A(:,2)       % entire column 2 -> [20; 50; 80]
A(1:2,2:3)   % submatrix -> [20 30; 50 60]

% Logical indexing (very useful)
mask = A > 50;
A(mask) = 999; % replaces all elements > 50 with 999



% Horizontal and vertical concatenation
C = [1 2 3];     D = [4 5 6];
H = [C D];       % [1 2 3 4 5 6]  (1x6)
V = [C; D];      % 2x3 matrix (rows stacked)

% Reshape (fill by column, preserve #elements)
E = reshape(1:12, 3, 4); % 3x4 matrix: columns filled 1..12
E(:) = 0;                % set all entries to zero (linear indexing)



v = (1:3)';           % 3x1
M = repmat(v, 1, 4);  % 3x4 by repeating columns of v

% Modern MATLAB (R2016b+): implicit expansion (broadcasting)
w = 10:10:40;         % 1x4
S = v + w;            % 3x4: each row adds w automatically


clear; clc;

A = [1 2; 3 4];
B = [5 6; 7 8];

A + B        % element-wise addition
A - B        % element-wise subtraction
2*A          % scalar multiply
A/2          % scalar divide (same as A * 0.5)
A.^2         % element-wise power (square each entry)


A .* B   % multiply each entry
A ./ B   % divide each entry
A .^ 2   % square each entry

A * B    % matrix product
A \ b    % solve Ax = b
A ^ 2    % A * A (square)

clear; close; clc;

A = rand(3,4);     % random 3x4
size(A)            % [3 4]
length(A)          % max(dim) -> 4
numel(A)           % # of elements -> 12
sum(A,1)           % column sums (1 keeps rows)
sum(A,2)           % row sums (2 keeps cols)
mean(A,1)          % column means
max(A,[],2)        % row-wise max


%%
clear; close; clc;


% Define matrix and vector
A = [1 0.5; -0.2 1];
b = [100; -50];

% Preferred solution
x = A\b;     % returns [Y; i]

% Alternative (less efficient)
x_inv = inv(A)*b;

disp(x)      % show solution
disp(x_inv)

% Compare the difference
diff_norm = norm(x - x_inv);
fprintf('Difference between methods: %.3e\n', diff_norm);

% Check residuals
r = norm(A*x - b);
r_inv = norm(A*x_inv - b);

fprintf('Residual with A\\b : %.3e\n', r);
fprintf('Residual with inv(A)*b : %.3e\n', r_inv);

%%
clear; close; clc;

A = [1 2; 3 4];

detA = det(A)      % determinant
rA   = rank(A)     % rank
Ainv = inv(A)      % inverse (compute only if needed)


clear; close; clc;

A = [0.8 0.1; 0.2 0.9];
lambda = eig(A)         % eigenvalues
[V, D] = eig(A)         
% columns of V = eigenvectors, D diagonal of eigenvalues

% Check spectral decomposition: A \approx V*D/V
A_recon = V * D / V;    % should match A up to numerical error


% Quick diagnostics
s = svd(A);        % singular values
kappa = cond(A);   
% condition number; watch out if kappa is large (e.g., 1e10)

clear; close; clc;

T = [0.85 0.10; 0.15 0.90];
eigT = eig(T)     % check magnitudes


Sigma = [1.5 -0.9; -0.9 2];
L = chol(Sigma, 'lower');
L * L'