% Jake Bobowski
% July 27, 2017
% Created using MATLAB R2014a

% This tutorial will show you how to do basic matrix manipulations.  This
% is where MATLAB really shines.  MATLAB, after all, stands for Matrix
% Laboratory.

clearvars

% Here's how you enter, for example, a 3x3 matrix.  Elements along the row
% are separated by comma.  The end of a row is indicated using a semicolon.
m = [1, 3, 0, 4; 2, -2, 1, 2; -4, 1, -1, -7]

% We can get the size of the matrix.
size(m)

% Size has a two-component output.  Here's a trick we can use to get the 
% number of rows assigned to a and the number of columns assigned to b.  
[a b] = size(m)

% We can now index m to select particular rows or columns or elements.
% Here's the element in the 2nd row, 3rd column.
m(2, 3)

% Here's entire 2nd row.
m(2, :)

% Here's the entire 3rd column.
m(:, 3)

% I can assign a new value to a specific element.
m(2, 2) = 99;
m

% I can also assign new values to an entire row...
m(2, :) = (10:10:40);
m

% ... or column.
m(:, 3) = (-10:-10:-30);
m

% m is a 3x4 matrix.  Let's define a 4x3 matrix
n = [1, 2, 3; 4, 5, 6; 7, 8, 9; 10, 11, 12]

% Now we can multiply our 3x4 matrix and or 4x3 matrix to create a 3x3
% matrix...
m*n

% ...or a 4x4 matrix.
n*m

% Just a word of caution.  You can also do element-by-element
% multiplication of two of matrices by placing a dot in front of *, as 
% in .*, see the example below:
x = [1:3; 4:6]
y = [10:10:30; 40:10:60]
x.*y

% Let's create a square matrix so that we can take its inverse.  MATLAB
% will automatically create 'magic nxn matix' using magic(n).  In a magic
% matrix all the rows, columns, and diagonals add up to the same number.
m3 = magic(3)

% Here's the inverse of m4.
im3 = inv(m3)

% We can check that the matrix product of m3 and im3 produces the identity
% matrix.
m3*im3

% It's also easy to take the transpose of matrices.  Of course, the
% transpose of a magic matrix is still magic!  Notice the dot (.) in front
% of ', without the dot MATLAB would transpose the matrix and then take the
% complex conjugate of each element.  It doesn't matter for a matrix in
% which all of the elements are real, but it would be important if you had
% some complex-values elements.  See below when we discuss the Hermitian
% Conjugate.
m.'
m3.'

% Someone in a qunatum mechanics course may want to take the Hertitian
% conjugate of a matrix.

% First, let's create a complex-valued matrix.
mc = [i, 3*i, 4; 2, -2, -2*i]

% The Hermitian Conjugate can be calcuated using...
ctranspose(mc)

% ...or simply by:
mc'
% Notice that, as stated above, the ' operator transposes and conjugates
% the matrix.