% Matlab Review Session
% CS229, Autumn 2006-07

% Basic Commands

% elementary operations
5+6
3-2
5*8
1/2
2^6
exp(4) 
log(exp(5)) 
abs(-3)

% variable assignment
a=3 
b=2
c=a^b
a=6  % overwrite
% semicolon to suppress output (not required)
d = a^2;


% matrices
m = [1 2; 3 4];
n = [5, 6; 7, 8];
p = ones(2,2)  % 2x2 matrix of ones
p = 4*ones(2,2) % trick to get matrix of all 4's
p = zeros(3,2)
eye(4)  % eye-dentity :)

% matrix indexing
m(1,2)   % m(i,j) is ith row, jth column
         % indexing starts from 1, not 0!
m_first_row = m(1,:)
m_first_col = m(:,1)

% matrix operations
m'  % transpose
inv(m)  % inverse
m*n  % matrix mult (dims. must agree)

m.*n % element-wise 
m./n 
m.^2
1./m


% vectors 
v = [1 2 3 4]  % same as 1D matrix

v = 1:4  % or [1:1:4]
x = 1:0.5:4  % or [1:0.5:4]

x =-10:0.01:10; % e.g. axes
y = 0.2*x+0.5;

% plotting
plot(x,y)

z = 1./(1+exp(-x)); % sigmoid
hold on; 
plot(x,z,'r');

axis([-5 5 0 1]);
xlabel('time');
ylabel('cost');
legend('model 1','model 2');
title('Hello');
% try subplot

% random numbers
rand  % uniform [0,1]
randn % normally distribd
randn(4,4)


% dimensions
size(m)
length(x)

% check variables defined
who
whos
save 'allvars.mat'
save 'x.mat' x
clear
load allvars
who
clear
load x

