
% An easy way to produce a plot of the cosine function
% from 0 to 12*pi is to use ezplot:
ezplot('cos(x)',[0,12*pi])

% Clear the command window:
clc

% Produce a vector of function values for
% y = f(x) = 1-tanh(3*x/5) for
% x ranging from - 5 to 5 in steps of 0.05:
x = -5:0.5:5;       % row vector
len = length(x),    % length of vector
y = 1-tanh(3*x/5);  % row vector of length len

% Find the dimensions of x:
size(x)

% Store the dimensions of x in variables m & n
[m,n] = size(x)

% Convert x & y into columns and 
% display them side by side:
disp([x' y'])

% You can also create a len x 2 matrix and
% display the result:
XY = [x' y'];
disp(XY)

% vector y can also be generated using a loop
for n=1:len
y1(n) = 1 - tanh(3*x(n)/5);
end
size(y1)
% Note - y1 generated this way is a row vector
% This is a built-in characteristic of MATLAB.
% Recall: 1:0.05:3 is a row vector (not a column).

% To generate a column vector for y,
% you can initialize it to a column of zeros
% before reassigning its values:
y2 = zeros(len,1)
for n=1:len
y2(n) = 1 - tanh(3*x(n)/5);
end
size(y2)
% A simplier way is this
for n=1:len
y3(n,1) = 1 - tanh(3*x(n)/5);
end
size(y3)

% To check to see if two vectors are equal
y2 == y3
% should give 1 if they are equal, & 0 otherwise
% But if roundoff may prevent them from exactly
% equal to each other, then you may want to try
max(abs(y2-y3))
% should give 0 if they are identical, and
% should give a number close to the machine #
% eps in case of a little roundoff.

% Create a row vector with the 1st element
% given by 2, the second element 2+3, etc.
% in steps of 3 until getting to 10.
Vec = 2:3:10
% Omit the step size if it is equal to 1:
Vec1 = 2:10
% Step size can be negative:
Vec2 = 10:-1:1

% Scalar can be used to multiply a vector 
% (or matrix): (each element of the vector is
% then multiplied by the scalar)
Vec3 = 2*Vec2;

% create a vector using the random number generator:
Rd = rand(1,7)
% access the second element:
Rd(2)
% access the last element of a vector
Rd(end)
% access the next to the last element of a vector
Rd(end-1)
% change the 1st element to 666
Rd(1)=666
%
disp(Rd)
% Extending a vector by assigning values to 
% an out-of-range element (unspecified elements
% take on the value of 0:
Rd(9)=999; disp(Rd);

% Accessing more than just one element from
% a vector or a matrix:

% using a vector of indices to access multiple
% elements of a vector:

% accessing the 2nd to the 4th element in Vec1
Vec1(2:4)
% accessing element 2, 4, etc until the end
% of a vector:
Vec1(2:2:end)
% accessing element 1, 3, etc until the end
% of a vector:
Vec1(1:2:end)
Vec1
RD = rand(5)
% accessing the 3rd column of a matrix
RD(:,3)
% accessing the 2nd row of a matrix
RD(2,:), RD
% accessing the 3rd to the 5th element 
% in row 2 of a matrix:
RD(2,3:5), RD

% Building up matrices from smaller ones:

% Recall that RD is 5 x 5 square matrix 

% Extending a matrix by assigning values to 
% an out-of-range elements (unspecified elements
% take on the value of 0:
RD(5,1) = 55
% another example:
RD(7,7)=77

% Recall row vectors Vec1 & Vec2:
disp(Vec1)
disp(Vec2)
% Create new row vector by joining Vec1 & Vec2
Vec5 = [ Vec1 Vec2]

% Recall that Rd and Vec1 are row vectors
% having the same length
disp(Rd)
disp(Vec1)
% put them one on top of the other to form
% a matrix of size 2 x length(Rd):
MTX1 = [ Rd; Vec1 ]
% This is illegal since the individual matrices
% have different widths:
%MTX11 = [ Vec2; Vec5 ] 
% comment out the above line & rerun script
% This is OK:
MTX11 = [ Vec2 Vec5 ]
% This is fine too:
MTX2 = [MTX1; Rd]
% MTX2 has size 3 x length(Rd)
% Thus we can build up large matrices from
% smaller ones.

% Deleting elements from a matrix
% Deleting the 3rd column from a matrix by
% setting it to an empty matrix []:
disp(RD)
RD(:,3) = []
disp(RD)

% Deleting the 1st row from Rd:
RD(1,:) = []
disp(RD)

% Note: after deleting any elements,
% the resulting matrix must form a matrix
% otherwise the operation is illegal.
% The following statement is not legal:
%RD(2,2:3) = [];
