% history091008.m
% K. Ming Leung, Sptember 25, 2008.
clear;
% run the RandIntegers script file to produce random integers.

% The is an ezplot function to create easy plots:
help ezplot
ezplot('cos(x)',[0,12*pi])

% To clear the command screen:
clc

% computing an 1D array (vector) of y values for each value
% in array x:
x = -5:0.5:5; len = length(x), y = 1 - tanh(3*x/5);
% length(x) gives the length of a vector
% Both x and y are row vectors.
% Vector y is computed all at once.
% This is called vectorized computation and is the fastest and most
% convenient way to get y.
% A slower and less convenient way is to use a loop,
% as shown below.

% The size of a matrix is given by:
size(x)
% The number of rows, n, and columns, m, of a matrix can be obtained:
[m,n]=size(x)
% display the two row vectors:
disp([x' y'])
% One can combine both vectors in the form of a matrix:
c = [x' y']
% and display the result:
disp(c)

% Computing vector y from vector x using a loop:
for n = 1:len
y(n) = 1 - tanh(3*x(n)/5);
end
disp(y)
% You see that y is a row vector.
% If you want y to be a column vector (and you don;t want to have to
% transpose the previous result), you can pre-assign y as a column
% vector of zeros:
y = zeros(len,1)
% Now this will get a column vector for y:  
for n = 1:len
y(n) = 1 - tanh(3*x(n)/5);
end
disp(y)

% The vectorizer version runs much faster than the one that uses a loop.
% A quick way to time a calculation is to use the tic--toc.
% Place the code that yo want to time between a tic and a toc.
% The run time will be displayed.
% For example here:
% vectorized version
tic;
x = -5:0.0001:5; 
y1 = 1-tanh(3*x/5); 
toc;
% version using a loop:
tic;
for n = 1:len
    y2(n) = 1 - tanh(3*x(n)/5);
end;
toc;

% To verify that both versions gives the same answer:
max(abs(y1-y2)) % should give 0.

% Create a 2 by 3 matrix of unifromly distributed random number
% between 0 and 1:
MTX = rand(2,3)
% Get different numbers in subsequent call to rand():
MTX1=rand(2,3), MTX2=rand(2,3)
% Get help on using the function rand:
help rand
