function multiplication
%function multiplication
%
% This demonstrates the "right" and "wrong" ways to do matrix
% multiplication in MATLAB.
%Author: Lina Arbach <lina@lina-arbach.com>

% Lines do not end in semicolons so that you can see what is going on

% First we create 2 row vectors for demonstration
vec1 = [1 2 3]
vec2 = [3 4 5]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% You want to do point by point multiplication. This should give you
% [1*3 2*4 3*5] = [3 8 15]
% First, doing it the wrong way
% To let the rest of the function run, you should comment this line
vec3 = vec1 * vec2

% Now do it the right way
vec3 = vec1 .* vec2

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% You want to do dot product multiplication

% wrong way 1, this is the same as before. The problem
% is you have a 1x3 vector times a 1x3 vector
%vec3 = vec1 * vec2

% wrong way 2, You remember you need to transpose one of the
% vectors, but you transpose the wrong one. This gives the outer
% product instead of the inner (dot) product
% you have a 3x1 vector times a 1x3 vector, giving you a 3x3 matrix
outer = vec1' * vec2

% right way
inner = vec1 * vec2'

% Note, the "right way" depends on the shape of your vectors. The
% example so far was for row vectors. If you have column vectors instead
% you need to transpose the other vector.

% Change the column vectors into row vectors. This is the more common
% mathematical form.
vec1 = vec1'
vec2 = vec2'

% Outer product
outer = vec1 * vec2'

% Inner product
inner = vec1' * vec2
