% Review.m
% Department of Radiology, MGH/Martinos
% Introduction to MATLAB programming
% Presenter: Adrian KC Lee
% 
% Date: November 16, 2010
% 
% Supplementary file

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB ENVIRONMENT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Commands to explore
% pwd       % Current directory
% cd        % Change directory
% ls        % List files in directory (same as Current Directory Tab)
% whos      % List all variables (same as Workspace Tab)
% path      % Gets or sets MATLAB search pah.
% addpath   % Include path in search path.
% clear all % clears all variable
% clear x y z % clear variables x y and z
% format    % changes display for results: See >> help format
% save      % save workspace variables to disk
% load      % load workspace variables from disk


%%%%%%%%%%%%%%%%%%%%%
% IMPORTANT COMMAND %
%%%%%%%%%%%%%%%%%%%%%
% help <function> % displays the text help file in command window.
% lookfor <keyword> % search all M-files for keyword.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATRIX MANIPULATIONS (1)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Row vector (Separator used: , or <space>)
a = [1,2,3]
b = [1 2 3]

% Column vector (Separtor used: ; or <Carriage Return>)
c = [1;2;3]
d = [1
    2
    3]

% Defining a matrix
A = [1 3 9; 5 10 15; 0 0 -5]
B = [1 3 9
    5 10 15
    0 0 -5]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATRIX MANIPULATIONS (2)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Row_2_Column_3_of_A = A(2,3) % Element in the 2nd row, 3rd column
A(3,1) = 2 % Correcting entry in 3rd row, 1st column

% Concatenation: Appending row and column vectors
D = [A;a]
E = [B c]
F = [A; c']
% F_error = [A;c] % Matrix dimension must match

% Submatrix manipulation using ':'
clear B
B = A(2:3,1:2) % Submatrix of A using range specifiers
C = A(2:3,:) % ':' specifies all rows or columns of the matrx
B(:,2) = [] % A row or column is deleted by setting it to a null vector.

% Look up eye, ones, zeros, diag, trace, det


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATRIX MANIPULATIONS (3)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Example of differences between Matrix and Array multiply
X = magic(3)
Y = magic(3)
X_matrix_multiply_Y = X*Y
X_array_multiply_Y = X.*Y

% Matrices vs Arrays operators:
% * vs .*; / vs ./; sprtm vs sqrt; expm vs exp

% General Maths functions
% +,-,*,/,^

% sin,asin; cos,acos; tan,atan;
% sinh,asinh; cosh,acosh; tanh,atanh;
% sec,csc,cot

% exp, log (denotes natural log), log10, sqrt

% Complex functions:
% abs, angle, conj, imag, real

% Round off functions
% fix   % Round towards 0.
% floor % Round towards -inf.
% ceil  % Round towards inf.
% round % Round towards the nearest integer
% rem   % Remainder after division. rem(a,b) = a - fix(a./b)
% sign  % Signum function

% Constants
% pi
% inf
% i,j   % imaginary number (sqrt(-1))
% eps   % Floating point relative accuracy. Good for avoiding dividing zero
% ans   % Most recent answer.
% NaN   % Not a Numbers


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CONTROL STATEMENTS 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Expressions in if / elseif or while loops, must be boolean
% These expressions are usually constructed by
% 1. Logical Functions:
% & (%AND) ,| (%OR) , ~ (%NOT) , xor (%exclusive OR)
% 2. Relative Operators
% <,<=,>,>=,==,~=

% END OF REVIEW.M