%------------------------------------------------------------------------------
% CS100J, Spring 2000
% Lecture 26
% Thurs 4/26
%------------------------------------------------------------------------------
  echo on
  clc
% Arrays
% + 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
%------------------------------------------------------------------------------
% Everything is an array
% + suppose want to solve something multiple times:
  sin(0), sin(pi/2), sin(pi), sin(3*pi/2), sin(2*pi)
  pause
% + slightly quicker way
  sin([0 pi/2 pi 3*pi/2 2*pi])
  pause
% is there an even quicker way?
% + colon (see $help colon$)
%   create vector: start:stop (increments by 1) 
%   create vector: start:inc:stop (increments by inc, but won't exceed stop)
%   examples)
    A = 0:pi/2:2*pi
    sin(A)
    % briefer:
    sin(0:pi/2:2*pi) 
% see also $help linspace$
% $linspace(start,stop,numberofvalues)$
    sin( linspace(0,2*pi,5) )
    pause
    clc
% + Miscellaneous
%   help zeros
%   help ones
%   help eye
%   help length
%   help size
%   help rand
%   help diag
%------------------------------------------------------------------------------
% Indexing
% + finding the location (or, position) of an element in an array
% + use index or subscript
%
%   A
%    ijk....
%   1st index (i) is 1st dimension (row)
%   2nd index (j) is 2nd dimension (col), 
%   3rd index (k) is 3rd dimension (page)
%   and so on....
%
% + useful help pages:
%   help paren
  clc
  pause

% + examples)
    A=['a' 'b' 'c'; 'd' 'e' 'f'] % create array of strings
    pause

% + single index extraction for 2D array (might seem peculiar)
% see $help colon$
pause
clc
A(:)      % get all elements of A and put in column
          % start at "first" element and work down columns, then rows
          % yes, I understand this might seem strange              
pause     
A(1)      % get first element from all the elements (as if in column)
pause
A(4)      % get 4th element from all the elements (as if in column)         
pause
A(1:3)    % get first thru third elements
pause

% + "regular extraction"
    A(1,2)    % get element at row 1, col 2
    pause

% + using a colon (this might hurt)
%   for now, assume 2D array
%   nameofarray(vectorofrows,vectorofcols)
%   vectors for rows and cols must not violate dimensions of A
%   (unless you are inserting something -- discussed in "Insertion" later)

clc
A([1 2],[1 2])
pause
A([1:2],[1:2])
pause
A(1:2,1:2)
pause
% A(1:2 1:2) % won't work!
%
% + more sophisticated use of colon
%   say "for these columns and these rows"
%
pause
clc
A(:,:)     % extract all rows and all cols
pause
A(:,1)     % for all rows of A, extract the elem from the 1st column
           % shorter: get 1st column from A
pause
A(1,:)     % for all cols of A, extract the first (or "top") elem 
           % for the 1st row of A, extract all cols
           % even shorter: get 1st row of A
pause
clc
% example)
B = rand(5,4)
pause
B([1 2 3],:)     % for all cols, get 1st thru 3rd row elements
                 % extract first three rows
pause
clc
B([3 2 1],:)     % extract 3rd, 2nd, 1st elements from each col
                 % extract first three rows in reverse order
pause
clc
B(:,[4 1])       % for all rows, extract 4th and 1st elements
                 % extract 4th and 1st columns
pause
clc
B([2 5],[4 2])   % extract elements with locations (2,4) (2,2) (5,4) (5,2)
                 % extract subarray
pause
clc
B([1 2 4],[3 1]) % from rows 1 2 4, extract cols 3 1
pause
clc
%------------------------------------------------------------------------------
% Insertion
clear
A=zeros(3,4)
pause
A(1) = 1
pause
A(2,3) = 23
pause
A(1,[1 2 3 4]) = [100 200 300 400]
pause
clc

% + when colon on left side of assignment, MATLAB takes elements from the
%   right side and inserts in the left-side matrix
A(2,:) =  [0.1 0.2 0.3 0.4]

% + can insert beyond dimensions!
%   MATLAB fills in needed "spots" with zeros
clear
clc
C=[1 2; 3 4]
C(6,6) = 10
%------------------------------------------------------------------------------
% exercise
Z=[1:3 ; 6:8]
% swap rows
Z([2 1],:) = Z([1 2],:)
pause
%------------------------------------------------------------------------------