%% adapted from MATLAB demo used in CS229 at Stanford University

%%% elementary arithmetic operations

5 + 6
3 - 2
5 * 8
1 / 2
2 ^ 6

%%% logical operations

1 == 2  % false
1 ~= 2  % true  NOTE: not "!="
true && false
1 && 0  % loose typing
1 || false

%%% variable assignment

a = 3
a = 7;              % semicolon suppresses output

a                   % to see value of variable, just type its name
a = 'hi'            % loose typing

%%% vectors and matrices

A = [ 1 2; 3 4; 5 6 ]
v = [ 1 2 3 ]
v = [ 1; 2; 3 ]
v = v'                % transpose of v
v = [ 1 : 0.1 : 2 ]   % from 1 to 2, with stepsize of 0.1 - useful for plot axes
v = 1 : 6             % from 1 to 6, assumes stepsize of 1
v = 6 : -1 : 1        % negative stepsize
w = zeros( 1, 3 )     % 1 x 3 vector of zeros
w = ones( 1, 3 )      % 1 x 3 vector of ones
C = 2 * ones( 2, 3 )  % same as C = [ 2 2 2; 2 2 2 ]
w = rand( 1, 3 )      % drawn from a uniform distribution on open interval ( 0, 1 ) 
w = randn( 1, 3 )     % drawn from a normal distribution ( mean = 0, var = 1 )
w = 1 + sqrt( 2 ) * ( randn( 1, 10 ) )  % ( mean = 1, var = 2 )
e = []            % empty vector
I = eye( 4 )      % 4 x 4 identity matrix

%%% matrix operations

% matrix multiplication
A
C
A * C
B = [ 5 6; 7 8; 9 10 ] % same dimensions as A

% element-wise multiplication
A .* B
% A .* C  or A * B gives error - wrong dimensions

A .^ 2

v
1 ./ v
log( v )  % functions like this operate element-wise on vectors or matrices 
exp( v )  % e ^ 1 ... e ^ 6
abs( v )

-v          % -1 * v

v + ones( 1, length( v ) ) 
v + 1       % same

A'          % transpose

%% miscellaneous useful functions

% max  (or min)
a = [ 1, 15, 2, 0.5 ]
[ val, ind ] = max( a )     % note multiple return values 

% find 
find( a < 3 )   % returns locations (indices) where expression evaluates to true

% sum, prod
sum( a )
prod( a )
floor( a )      % or ceil( a )

% check whether empty
isempty( e )

%% indexing
A
A( 3, 2 )   % indexing is ( row, col )  NOTE: MATLAB indexing starts at 1
A( 2, : )   % ":" means get every element along that dimension, so get the entire 2nd row
A( :, 2 )   % get the 2nd column
A( 1, end ) % 1st row, last element
A( end, : ) % last row

A( :, 2 ) = [ 10 11 12 ]'       % change second column
A = [ A, [ 100; 101; 102 ] ]    % append column vector

idx = find( a < 3 )             % numeric indexing
a( idx )
idx = ( a < 3 )                 % logical indexing
a( idx )
A
A( A > 11 )

%% dimensions
size( A )
size( A, 1 )    % number of rows
size( A, 2 )    % number of cols
length( v )     % size of longest dimension
length( A )

%% loading data
who
load 'miscellaneous MATLAB files/q1y.dat'
load 'miscellaneous MATLAB files/q1x.dat'
who
whos
clear q1y           % clear with variable names as arguments clears only those variables
% clear all         % clears all variables (including fid for script!)
who
v = randn( 1, 10 );

save hello v;       % save variable v into file hello.mat
clear v
who
load hello
who
save hello.txt v -ascii; % save as ascii

% fopen, fprintf, fscanf also work

ls      % various unix commands work
pwd
cd ./   % on Windows, can use '/' or '\' in pathnames
cd './' % put quotes around pathname if it contains spaces
        % to access shell, preface command with "!"

%% plotting
t = [ 0: 0.01 : 0.98 ];
y1 = sin( 2 * pi * 4 * t ); 
plot( t, y1 );
y2 = cos( 2 * pi * 4 * t );
hold on;            % 'hold off' to turn off
plot( t, y2, 'r' );
xlabel( 'time' );
ylabel( 'value' );
legend( 'y1', 'y2' );
title( 'my plot' );
close;              % or 'close all' to close all figures

figure( 2 );        % can specify the figure number
subplot( 1, 2, 1 );
plot( t, y1 );
subplot( 1, 2, 2 );
plot( t, y2 );
axis( [ 0.5 1 -1 1 ] );  % change axis scale

%% control statements

w = [];
z = 0;
for i = 1 : 10   w = [ w, 2 * i ];   z = z + i;   end
w
z
% % normal way to indent block
% for i = 1 : 10
%     w = [ w, 2 * i ];
%     z = z + i;
% end
% don't actually want to do this calculation this way
%   instead do w = 2 * [ 1 : 10 ], z = sum( [ 1 : 10 ] );

w = [];
while ( length( w ) < 3 )   w = [ w, 4 ];   end
w
% % normal way to indent block
% while ( length( w ) < 3 )
%     w = [ w, 4 ];   
% end

% if w( 1 ) == 0
%     % <statement>
% elseif w( 1 ) == 1
%     % <statement>
% else
%     % <statement>
% end

exit  % quit
