%------------------------------------------------------------------------------
% Miscellaneous topics
% see also the symbolics.m example

more on

% Random
rand           % random number between 0 and 1
rand(1) + 5    % random number between 5 and 6
10*rand(1) + 5 % random number between 5 and 15
%------------------------------------------------------------------------------
% string input
help input

s = input('Give me a string! ', 's'); % enter a string without having to use ''
s        % check the output (which will be just the character $s$
s == 's' % MATLAB does "know" that $s$ is indeed the string $'s'$
         % (the output is $1$ for true)
%------------------------------------------------------------------------------
% What is the difference between $eval$ and $feval$ ?

eval('cos(0)') 

% ans =
%
%     1
%

feval('cos',0)

%
% ans =
%
%     1
%
% Technically, $feval$ is more efficient for functions than $eval$
pause
%------------------------------------------------------------------------------
echo on
% How to do symbolic stuff? 2 options:
% 1) [not required!] go to CS99 2000FA webpage and go to Material page ;_
% 2) [required!] 
  help sym
  pause
  help syms
  pause
  help solve
  pause
% See also symbolics.m

echo off
%------------------------------------------------------------------------------
% Strings are collections of ASCII characters
s = 'AbCd'
% s = AbCd

% Characters are ASCII and have numerical codes (integers)
% convert to integer codes with $double$
d = double(s)
% d =   65    98    67    100

% convert to characters
c = char(d)
% c = AbCd

% 'a' to 'z' are ints 97:122
r1 = double('a'):double('z')

% 'A' to 'Z' are ints 65:90
r2 = double('A'):double('Z')

% 'a'-'A' (and 'b'-'B', etc) is 32
r1 - r2

% convert to uppercase
char('a'-32)
% ans = A
% see also $help upper$ and $help lower$
%------------------------------------------------------------------------------
% LOGICAL ARRAYS

s = 'AbCd'
% ans = AbCd
x = s < 97
% x = 1 0 1 0

% x is a LOGICAL ARRAY
% MATLAB applies relation s(ii) < 97 to each element in s
% (Imagine that you had a loop for $ii=1:length(s)$)
% MATLAB stores 1 or 0 depending on the truth of the relation for each element

% To extract elements that correspond to TRUE relations, use
% array(logicalarray)
s(x)
% ans = AC

% What happened? MATLAB looked at s as 4 elements   AbCD
%                MATLAB looked at x as 4 indices    1010
%                MATLAB took only the TRUE elements A C
%------------------------------------------------------------------------------
% Remainder (modulus, mod)
% To find a remainder from a division operation:
% example) 5/4 gives a remainder of 1
mod(5,4)
% ans = 1

% example) find even integers from an array
z = 0:10;         % array from 0 to 10
k = mod(z,2)==0;  % $k$ is a logical array
z(k)              % extract even integers from $k$
% ans = 0 2 4 6 8 10

% WARNING:
% to generate logical array, MUST use relations, not just enter 0 and 1!
% example)
z=3:5;     % array of ints from 3 to 5
q=[0 1 0]; % array of what appears to be logical values (but isn't!)
% z(q)     % attempt to extract elements from $z$ (but won't work)
% output is the error mesg: 
%   ??? Index into matrix is negative or zero. See release notes on changes to 
%   logical indices.
% To fix, use $logical$ function:
z(logical(q))
% ans = 4
%------------------------------------------------------------------------------





