%% Vector basics

% Row vector
x = [1 1 1 1]

% Column vector
y = [1 1 1 1]'

% create vector with sequence of numbers
z = [1:2:10]
z = [-1:0.01:1]

% Getting an element out of a vector
z(2)
z(2:3)

% looping over elements
for i = 1:4,
    display( x(i) );
end;

% Add or multiply values etc.
x + 10
2*x


%% Vector multiplication
% Transpose vectors
x
x'

% Vector multiplication: row vector * column vector (for time being)
x,y    % just for display
x*y

% Common error: multiply vectors of different lengths:
z = [1 1 1]'
x*z

% Define two parallel vectors:
x = [1 0 1 0]
y = [1 0 1 0]'

% Multiply vectors
x*y

% Define two orthogonal vectors:
x = [1 0 1 0]
y = [0 1 0 1]'

% Multiply vectors
x*y


%% Some things you can do with vectors 
% Define a vector
x = [1 2 3 4]

% Define another vector
y = [0 0 1 0]'   % note the transpose

% Multiply the vectors
x*y

% Define another vector
z = [1 1 1 1]'

% Sum of elements of x:
x*z

% Average of elements of x
% length(x) yields the number of elements of a vector
x*z / length(x)

% Define another vector
y = [1 -1 1 -1]'

% Subtract sum of even from sum of odd elements in x:
x*y

clear
%% Matrices

% Define a (diagonal) matrix
x = [1 1 1; 2 2 2]

% Getting elements out of matrices
x(1,2)
x(:,1)
x(2,:)

% Getting matrix dimensions
size(x) % returns number of rows and columns, respectively
[m,n] = size(x)

%% Matrices c'd
% Matrix transpose
x
y = x'

% adding/multiplying values etc.
x + 2
2 * x

% Define another matrix
z = [1 2 3; 4 5 6]

% Subtract z from x
x - z

% Add z to x
x + z

% Elementwise multiplication (NOT "matrix multiplication": "*")
x .* z

% Try... (matrix multiplication)
x * z

% Elementwise division (NOT "matrix division/inversion": "/")
x ./ z

% Try... (matrix inversion)
x / z

%% Multiplying matrix and vector
% Define a vector
v = [1 1 1]'

% Mutliply matrix x with vector v (and obtain sum within each row of x)
x
v
x * v

% matrix and vector dimensions must agree
v * x % does not work

%% interpretation as sum of column vectors
% x * v results in the sum of column vectors in x
x * v
1*x(:,1) + 1*x(:,2) + 1*x(:,3)

%% Matrix multiplication
% Multiply two matrices
x * y

% Multiply two matrices
y * x

% Note: x*y is not necessarily y*x!!!

% Define new matrix
z = [1 1; 2 2]

% Works:
y*z
z*x

% Doesn't work:
z*y
x*z

%% Make your own matrices

% Identity matrix
id = eye(3)

% Example:
x*id

id*x'

% diagonal matrix
di = eye(3)
di(1,1) = 1
di(2,2) = 2
di(3,3) = 3

% Multiplies columns by a factor:
x*di

% Multiplies rows by a factor
di*y

%% Some useful functions

% sum/average across columns of a matrix
% note: most Matlab function operate column-by-column
sum( x )
mean( x )

% sum across rows of a matrix
sum( x' )
mean( x' )

% get the diagonal of a (square) matrix
mydiag = diag(di)

% dimensions of a matrix, size()
[m,n] = size(x)

% dimension of a vector
diaglen = length(mydiag)

% create a matrix/vector with ones
ones(10)   % creates 10x10 matrix
ones(10,1) % creates 10x1 column vector

% create a matrix/vector with zeros
zeros(10)   % creates 10x10 matrix
zeros(1,10) % creates 1x10 row vector

% create matrix/vector with standard uniform random numbers in interval 0-1
rand(3)

% create matrix/vector with standard normal random numbers in interval 0-1
randn(3)

% "reshaping" a matrix or a vector
x = 1:10
reshape(x,5,2)
reshape(x,2,5)

% Kronecker product ("concatenates" matrices/vectors)
kron(id,y)

