% matrix_mathematics
% This script demonstrates how to apply mathematical operations to vectors 
% and matrices in Matlab.
% It is part of the Matlab course for PhD Students 2010-2011:
% http://www.sinnesphysiologie.uni-oldenburg.de/50246.html
% written by Jutta Kretzberg, 18.11.2010

%% Generation of variables to be used later

a=0;                    % scalar variables
b=-5;
c=17.5;

u=1:9;                 % vector variables
v=[1 -3 2];
w=[0 -0.5 1];

M=[1 4 -2; 0.5 -0.5 10] % 2-by-3 matrix
N=[0.5 6; 9.7 -5; 1 0]  % 3-by-2 matrix
O=[0 1 2; -1 -5 7; 2 -6 5] % 3-by-3 matrix

%% Scalar operations
% mathematical operations +, -, *, / of scalar values are performed as expected

s1=4+7              
s2=c+a
s3=c-9
s4=4.6*s3
s5=27/s1

%% Scalar-matrix operations
% In Matlab, scalars can be multiplied, divided, added and subtracted to 
% and from vectors and matrices. The operations are performed for each of 
% the vector or matrix elements.
% Element-by-element power is obtained by x.^y.
% The order of operations is given by the mathematical rules (* and / 
% before - and +). Otherwise, parentheses ( ) are used to control the order 
% of operations. 

sm1=2*M
sm2=N*b
sm3=O+c-8
sm4=(u/2.5)+0.1

sm5=u.^-1
sm6=2.^O

%% element-by-element matrix-matrix operations
% For addition, subtraction, and element-by-element multiplication and 
% division the combined vectors or matrices must have the same dimensions.


em1=v+w
em2=v/3-b*w
em3=M+N'

em4=v.*w
em5=M./N'

%% matrix multiplikaton

mm1=v*w'
mm2=w*v'
mm3=w'*v
mm4=v'*w

mm5=N*M
mm6=M*N

