format compact;
clc;

disp('Welcome To the Matlab Introduction Program');
disp('==========================================');
disp('  1. Matlab environment');
disp('  2. Matlab Data Objects');
disp('  3. Matrix and Vector Arithmetic');
disp('  4. Built in Functions');
disp('  5. Relations and Control Structures');
disp('  6. Plotting Graphs');
disp('  7. Script and Function M-Files');
disp('');
section=input('Select a Section [1-7]: ');
clc
    
if(section <=1);
    
disp('1. Matlab Environment');
disp('---------------------');
input('');
disp(' * Matlab has a command line interface. ')
disp(' * When writing a long matlab statement that exceeds a single row use ''...'' ')
disp('   to continue statement to next row.')
input('');
disp(' * When using the command line, a semicolon at the end means matlab will not')
disp('   display the result. If the semicolon is omitted then matlab will display result.')
input('');
disp(' * Use the up-arrow to recall commands without retyping them (and down');
disp('   arrow to go forward in commands).');
input('');
disp(' * Other commands borrowed from various shells:')
disp('   - C-a moves to beginning of line')
disp('   - C-e for end of line')
disp('   - C-k deletes the line to the right of the cursor')
disp('   - C-p goes back through the command history')
disp('   - C-n goes forward (equivalent to up and down arrows)')
input('');
disp(' Matlab is an interpreted language.');
input('');
disp(' Simple calculations. ');
input('');
input('>> 3*2');
3*2
input('');
disp(' * The basic arithmetic operators are + - * /, and ^.');
disp(' * The order precedence is the same usual.');
input('');
disp(' Define and use named variables.')
input('>> deg = pi/180');
deg=pi/180
input('');
disp(' * The type of the variable does not need to be defined, unlike in C/Java.');
input('');
input('>> 1.2 * sin(40*deg + log(2.4^2))');
1.2 * sin(40*deg + log(2.4^2))
input('');
input('>> new = 3*ans');
new = 3*ans
input('');
input('>>3*0.7662');
3*0.7662
input('');
disp(' * An appreciation of rounding errors is important!');
input('');
disp(' See the variables that you defined');
input('>>whos');
whos
input('');
disp(' Clear the variables');
input('>>clear new');
clear new
input('');
disp(' Use ''format <type>'' to get output to different precisions')
input('')
input('>> p=3.1415926535')
p=3.1415926535
input('')
input('>> format long')
input('>> p')
format long;
p
input('')
input('>> format short')
input('>> p')
format short;
p
input('');
disp(' Save data to file');
input('>> save degfile deg')
input('');
disp(' Load data from file');
input('>> load degfile')
input('');
disp(' To get help type "help" (will give list of help topics) or "help <topic>"');
input('');
input('>> help');
help
input('')
input('>> help sqrt');
help sqrt
input('')
disp(' * If you don''t know the exact name of the topic or command you are looking for,');
disp('   type "lookfor keyword" (e.g., "lookfor regression")');
input('');
disp(' Funny commands.');
input('>> why');
why
input('');
clc
end

if(section <=2);
clc
disp('2. Matlab Data Objects');
disp('----------------------');
input('');
disp('Define a scalar:')
input('>> n=5');
n=5
input('');
disp('Define a row vector: ')
input('>> r  = [1 2 3]');
r = [1 2 3]
input('');
disp('Define a column vector: ')
input('>> c = [4;5;6]');
c = [4;5;6]
input('');
disp('Transpose a vector (row to column or column to row):');
input('>> c=c'' ');
c = c'
input('>> r=r'' ');
r = r'
input('');
disp('Define a vector using the colon notaion: v=start:stepsize:end');
input('>> v=1:.5:3');
v=1:.5:3
input('>> v=10:-1:1');
v=10:-1:1
input('');
disp('The empty vector:');
input('>> v=[]');
v=[]
input('');
disp('Define a matrix with the [ ] operator');
input('>> m = [1 2 3; 4 5 6]');
m = [1 2 3; 4 5 6]
input('');
disp(' * Matrices in matlab are such that 1ST parameter is ROWS');
disp('   and the 2ND parameter is COLS');
input('');
disp(' * Unlike C/Java, matlab indices start at 1');
input('');
disp(' * Unlike C/Java, matlab matrices expand automatically');
input('');
disp('Access matrix elements, rows, and columns:')
input('>> m(1,1)');
m(1,1)
input('>> m(2,3)');
m(2,3)
input('>> m(1,:)');
m(1,:)
input('>> m(1:2,2)');
m(1:2,2)
input('>> m(1,1)=10');
m(1,1)=10
input('>> m(1,:)=[10 11 12]');
m(1,:)=[10 11 12]
input('>> m(4,4)=7');
m(4,4)=7
input('');
disp('Define a zeros/ones matrix/vector:');
input('>> m=zeros(3,4)');
m=zeros(3,4)
input('');
input('>> m=ones(2,3)');
m=ones(2,3)
input('');
disp('Identity matrix of size NxN:');
input('>> m=eye(3)');
m=eye(3)
input('');
disp('Random matrix of size NxM:');
input('>> m=rand(2,3) ');
m=rand(2,3) 
input('');

disp('Concatenate two vectors into a matrix');
input('>> a=[1;2;3], b=[4;5;6]');
a=[1;2;3], b=[4;5;6]
input('>> m=[a,b]');
m=[a,b]
input('');
disp('Concatenate a matrix and a vector');
input('>> c=[7,8]')
c=[7,8]
input('>> m=[m ;c]')
m=[m ;c]
input('');
disp('Get the size of a matrix or vector:')
input('>> m = [1 2 3; 4 5 6]')
m = [1 2 3; 4 5 6]
input('>> size(m)')
size(m)
input('>> num_rows=size(m,1) ')
size(m,1)  %num rows
input('>> num_cols=size(m,2) ')
size(m,2)  %num columns
input('')
clc
end

if(section <=3);
disp('3. Matrix and Vector Arithmetic')
disp('-------------------------------')
input('');
disp(' * Follow conventional linear algebra rules');
disp(' * Pay attention to matrix and vector sizes when performing operations');
disp(' * Mismatched sizes will result in errors. Use size() to check');
disp(' ')
input('>> a=[1 2 3 4], b=[5 6 7 8]')
a=[1 2 3 4], b=[5 6 7 8]

disp('Scalar-vector multiplication:');
input('>> 2*a')
2*a
input('');
disp('Vector-vector addition:');
input('>> a+b')
a+b
input('');
disp('Standard Vector-vector multiplication (inner product):');
input('>> a*b'' ')
a*b'
input('');

disp(' * Some operators also work elementwise via ''dot'' modifier (.* ./ .^)');
disp(' ')
disp('Elementwise vector-vector multiplication:');
input('>> a.*b')
a.*b
input(''); 
disp('Elementwise exponentiation:');
input('>> a.^2')
a.^2
input(''); 
clc
end

if(section <=4);
disp('4. Built In Functions');
disp('---------------------');
input('');
disp(' * Matlab has a large number of built in functions');
disp(' * ''help'' will list categories');
input('');
disp('Elementwise functions on vectors or matrices:');
input('>> log([1 2 3 4])')
log([1 2 3 4])
input('');
input('>> round([1.5 2; 2.2 3.1])')
round([1.5 2; 2.2 3.1])
input('');
disp('Some functions alter the dimension of matrices:');
input('>> a=[1 4 6 3]')
a=[1 4 6 3]
input('>> sum(a)')
sum(a)
input('>> mean(a)')
mean(a)
input('>> var(a)')
var(a)
input('>> std(a)')
std(a)
input('>> max(a)')
max(a)
input('');

input('>> m=[1 2 3; 4 5 6]')
m=[1 2 3; 4 5 6]
input('>> mean(m)')
mean(m)
input('>> max(m) ');         
max(m)                       %max of each column  
input('>> max(max(m))');   
max(max(m))     %to obtain max of matrix
input('');
clc
end

if(section <=5);
disp('5. Relations and Control Structures');
disp('-----------------------------------');
input('');

disp('Relational operators: == (equal), ~= (not equal), <, <=, >, >=')
disp(' * All operate componentwise!');
input('>> a=[1 2 3], b=[1 3 2]'); 
a=[1 2 3], b=[1 3 2]
input('>> a==a'); 
a==a
input('>> a==b'); 
a==b
input('>> a~=b'); 
a~=b
input('>> a>b'); 
a>b
input('>> b<=a'); 
b<=a
input('');

disp('Standard control structures:');
disp(' * if-elseif-else-end');
disp(' * for-end');
disp(' * while-end');
disp(' * switch-case-end');
disp(' * break');
input('');

disp('>> x=4')
disp('>> if x>10');
disp('y=x^2');
disp('else');
disp('y=log2(x)');
disp('end');
input('');
x=4;
if x>10
  y=x^2;
else
  y=log2(x);
end
input('');
y
input('');


input('>> for i=1:5; y(i)=i^2; end');
for i=1:5; y(i)=i^2; end;
input('>> y')
y
input('')

input('>> x=1;while (x<8); y(x+1)=sin(x);x=x*2; end');
x=1;while (x<8); y(x+1)=sin(x);x=x*2; end
input('>> y')
y
input('')
clc
end

if(section <=6);
disp('6. Plotting Graphs');
disp('------------------');
input('');

disp('Basic y vs x plot using vectors of points')
input('');
input('>> x=[0 1 2 3 4]');
x=[0 1 2 3 4]
input('>> y=2*x');
y=2*x
input('>> plot(x,2*x);');
plot(x,2*x);
input('')

disp('y vs x plot with labels and title')
input('');
input('>> figure');
figure;
input('>> x=pi*[-24:24]/24');
x=pi*[-24:24]/24
input('>> y=sin(x)');
y=sin(x)
input('>> plot(x,y);');
plot(x,y);
input('>> xlabel(''radians'');');
xlabel('radians');
input('>> ylabel(''sin value'');');
ylabel('sin value');
input('>> title(''A graph'');');
title('A graph');
input('')

disp('Several y vs x plots in one figure')
% several graphs in one plot:
input('>> figure');
figure;
input('>> x=pi*[-24:24]/24');
x=pi*[-24:24]/24
input('>> hold on');
hold on;
input('>> plot(x,sin(x),''b-'');');
plot(x,sin(x),'b-');
input('>> plot(x,2.*cos(x),''ro'');');
plot(x,2.*cos(x),'ro');
input('>> legend(''sin'', ''cos'');')
legend('sin', 'cos');
input('>> hold off');
input('');

disp('Matrices as Images')
input('>> figure');
figure;
input('>> m=rand(8,5);');
m=rand(8,5)
input('>> imagesc(m)');
imagesc(m)
input('>> colormap(''gray'');');
colormap('gray');
input('');
clc
end

if(section <=7);
disp('7. Script and Function M-Files');
disp('------------------------------');
input('');

disp(' * A script m-file just contains a list of commands to run');
disp(' * You must be in the directory where your function file is stored to run it.')
disp(' * Use ''cd'' and ''ls'' to move around in the file system.') 
disp(' * A function m-file starts with: function <outputs> = <function name>(<inputs>)');
input('');

input('>> ls');
ls
input('');
input('edit poly');
edit poly;
input('')
input('>> poly(5)')
y=poly(5);
input('')

input('>> poly([1 2;3 4])')
y=poly([1 2;3 4]);
input('')
end

input('')
disp('Matlab tutorial link:');
disp('http://www.cs.ubc.ca/~nando/cpsc530a/handouts/cam_matlab.pdf');
