%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%        TYPES AND VARIABLE DEFINITIONS  %%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Basic types and their definitions: the basic objects in matlab are
% scalars, vectors, and matrices... But you can also have characters or strings
x               = 0		% scalar
input_char = 'y' % char
input_string = 'hello'; % string

who % lists the variable in the workspace
whos % same as who, but with additional memory status
save a.mat % saves all variables to a file 'a.mat'.
% File name is 'matlab.mat' by default

clear % clears the workspace of all variables
whos % same as who, but with additional memory status
load a.mat      % loads 'a.mat' into current workspace
% File name is 'matlab.mat' by default
whos % same as who, but with additional memory status

% To define a matrix:
%         -- Separate elements in a row with spaces or commas
%         -- Separate columns using semi-colons, or newlines
%         -- Enclose everything in square brackets


A = [1 2 3; 4 5 6] % Creates a 2x3 matrix
v = [1 0 0]			% a row vector
v = [1;2;3] % a column vector
v = [1:.5:3] % a vector in a specified range:
v = pi*[-4:4]/4 % [start:stepsize:end]
v = [] % empty vector

% Matrix initialization functions:
m = zeros(2,3) % creates a 2x3 matrix of zeros
v = ones(1,3) % creates a matrix of ones
m = eye(3) % identity matrix
v = rand(3,1) % rand matrix

size(A)					% returns the size of a matrix
size(A,1) % number of rows
size(A,2) % number of columns
m = zeros(size(A)) % create a new matrix containing the size of A
m = [m; [9 8 7]] % add a new row to matrix m
m = [m, [11; 12; 13]] % add a new column to matrix m

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


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%% SIMPLE OPERATIONS %%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Accessing elements:

% IMPORTANT: MATLAB uses 1-based indexing. The first element is A(1), the second A(2) etc.


A(1,3) % access a matrix element
% matrix(rownumber, columnnumber)
A(1,3)=9 % assigning value to a matrix element
v(3) % access a vector element

A(3,1) % error for exceeding the dimension for r-value
A(3,1)=5 % extends the matrix when dimensions are exceeded for l-value

A(2,:) % access a matrix row (2nd row)
A(:,1) % access a matrix column (1st row)
% Once the first column is exhausted, the linear ordering continues at the beginning 
% of the second column.
A(2)
A(4)
% Printing: C format
fprintf('%d\n', A(1,3));
fprintf('%d\n', A(2,:));

% Pointwise operations: Operations that work element by element


2 * A % scalar multiplication
v / 3 % scalar division
v + w % pointwise vector addition
v .^ 2 % pointise vector squaring (note .)
v .* w % pointwise vector multiply (note .)
log( [1 2 3 4] ) % pointwise arithmetic operation: log
round( [1.5 2; 2.2 3.1] ) % pointwise arithmetic operation: round


% Vector operations
v 	= v'				% transpose a vector (row to column or column to row)
A' % transpose a matrix
sum(v) % sum a vector
sum(A) % sum of the columns of a matrix
sum(A,1) % sum of the columns of a matrix (default)
sum(A,2) % sum of the rows of a matrix
mean(v) % mean of a vector
var(A) % variance of the columns of a maxrix
std(A) % standard deviation of the columns of a matrix
max(A) % max values of the columns of a matrix
max(max(A)) % to obtain max of matrix
max(A(:)) % or...


% Matrix operations
size(B)
size(A)
B * A % matrix multiplication
A * B % dimension clash
diag(A) % returns the diagonal elements of a matrix
inv(A) % inverse matrix
eig(A) % finds eigenvalues of A
A = [1 2 3 4; 5 6 7 8; 9 10 11 12]
reshape(A,6,2) % reshapes the matrix A