%-------------------------------------------------------------------------------
% lecture6
%-------------------------------------------------------------------------------
  clear
  more on
  more off
  clc
  echo on
% echo off
%-------------------------------------------------------------------------------
  clc
% Arrays
% + store multiple values in one conveeeeenient package
% + think tables or spreadsheets
% + array stores expressions in a rectangular grid with rows and columns
% + MATLAB's law: "everything an array"
% + sometimes say MATRIX
%   - an array is a more general quantity
%   - MATLAB classifies some operations as array and others as matrix
  pause
%-------------------------------------------------------------------------------
  clc
% Scalar
% + 1 row and col
% + "0-dimensional" array
  1, [1], [[1]]  % MATLAB will remove redundant []s
                 % So, all output is just 1
  pause
%-------------------------------------------------------------------------------
  clc
% One-Dimension
% + one row or one column
% + MATLAB stores information as rows, usually
%   - to separate individual elements, use spaces or commas
%   - to separate individual rows, use semicolons
  [1 2 3] % 1-D array (row)
  [1;2;3] % 1-D array (col)
  pause
%-------------------------------------------------------------------------------
  clc
% Two-Dimension
% + multiple rows and columns
% + also called rectangular
% + see vectors for separating rows and columns
  [1 2; 3 4] % square matrix with rows [1 2] and [3 4]
  [1,2; 3,4] % same as [1 2; 3 4]
  pause
% + rectangularity must be preserved
  %  [1,2; 3] % fails because second row has only 1 element
  pause
% appending arrays
  A=[1 2; 3 4]
  [A,A] % creates an array of 4 columns and two rows
  pause
%-------------------------------------------------------------------------------
  clc
% Transpose
% + to store using columns, gets a bit more complicated
% + use transpose operator $'$
% + 1-D:
%   ri' = rj (rows become columns and vice versa)
    r=[1 2 3]
    r'
    pause
% + 2-D:
%   Aij = Aji
    A
    A'
    pause
    r1 = [1 3]; r2 = [2 4];
    [r1' r2']
    pause
%-------------------------------------------------------------------------------
  clc
% Matrices
% + same structure and look as an array
% + usually means rectangular or square, but could have more dimensions
% + a matrix is an array that represents a linear transformation
%  
%   Ax = b
%   A: coefficient matrix
%   x: solution vector
%   b: source vector
%
%   example)
%    2x-2y = 10 \     [ 2 -2] {x} = {10}
%                >->  [-2  5] {y}   {20}
%   -2x+5y = 20 /        A     x     b
%
%   A transforms x into b
%   Find x from solution of simultaneous equations
%   (save for later --> see MATLAB Handouts online)
% pause
%-------------------------------------------------------------------------------
  clc
% Conditional Statements
% + also known as selection statements
% + so far, programs seen as top-down sequence of instructions
% + often problems require choices
% + need to express situations as TRUE and FALSE
% + use tests to cause branches in the program
  pause
%-------------------------------------------------------------------------------
  clc
% Logic
% + use to test situations
% + tests called RELATIONS
%   value op value
% + MATLAB evaluates the statement for truth
% + answer is either 1 (true) or 0 (false)
% examples) 
  1 < 2   % less than
  1 == 1  % equal (true)
  1 == 2  % equal (false)
  % source of confusion:
  %   $==$ is a comparison for equality
  %   $=$  is an assignment operator
  pause
% logical operators: & (and), | (or), ~ (not), xor (exclusive or)
  1==1   &   2==2
  1 > 2  |  1 < 2
  xor(1 < 2,1 < 3)
  pause
%-------------------------------------------------------------------------------
  clc
% Branches
% + making choices
% + use $if$ (skip $switch$)
% + see $help if$
%   General form of the $if$ statement is
% 
%      IF expression
%        statements
%      ELSEIF expression
%        statements
%      ELSE
%        statements
%      END
%
% + Must have $if$ and $end$
% + $else$ and $elseif$s are optional
  pause
%-------------------------------------------------------------------------------
  clc
% Example 1
% Test if a number is positive
  x = input('Enter a number: ');
  if x > 0
     disp(['Your number is positive.'])
  end
  pause
%-------------------------------------------------------------------------------
  clc
% Example 2
% Test if a number is positive
  x = input('Enter a number: ');
  if x > 0
     disp(['Your number is positive.'])
  else
     disp(['Your number isn't positive.'])
  end
  pause
%-------------------------------------------------------------------------------
  clc
% Example 3
% Test sign of a number (see $help sign$ for built-in function)
  x = input('Enter a number: ');
  if x > 0
     disp(['Your number is positive.'])
  elseif x < 0
     disp(['Your number is negative.'])
  else
     disp(['Your number is zero.']) % but can you guarantee that???
  end
  pause
%-------------------------------------------------------------------------------
  clc
% Example 4
% Test sign of a number (see $help sign$ for built-in function)
  x = input('Enter a number: ');
  if x > 0
     disp(['Your number is positive.'])
  elseif x < 0
     disp(['Your number is negative.'])
  elseif abs(x) < eps
     disp(['Your number is zero.']) % but can you guarantee that???
  else
     disp(['I don't know what the hell your number is.'])
  end
  pause
%-------------------------------------------------------------------------------
  clc
% Example 5
% Test sign of a number
  test = input('Enter the number to test: ');
  if isnumeric(test)
     if x > 0
        disp(['Your number is positive.'])
     elseif x < 0
        disp(['Your number is negative.'])
     elseif abs(x) < eps
        disp(['Your number is zero.'])
     else
        disp(['I don't know what the hell your number is.'])
     end
  else
     disp(['I said "Enter a number." Now look what you have done!'])
  end
  pause
%-------------------------------------------------------------------------------
