%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Following: MATLAB An Introduction with
%            Applications: Amos Gilat
%
Chapter 2   Creating Arrays
%
% Note: As before, to ensure portability, all of the 
% commands below were actually executed within 
octave rather than MATLAB
%
% Also, despite the title of the Chapter, much of what
% follows has to do with the manipulation
% of arrays, in addition to their creation.
%
% We should first observe that arrays are by far the 
% most important data object (data structure) in 
% MATLAB, and the strong support the language provides
% for the creation and manipulation of arrays is a 
% major reason that it has become so popular for 
% numerical computations.  As I have mentioned 
% previously, many, if not most, basic techniques in
% numerical analysis (as well as many advanced topics)
% are naturally expressed in the language of vectors
% and matrices, and thus can often be naturally 
% expressed in MATLAB.
%
% Importantly, MATLAB provides the means to perform
% operations on arrays as a whole, in addition
% to mechanisms for working with individual elements
% of arrays.  The former approach generally leads 
% to a more concise expression of a given algorithm
% (for example, many for loops that might 
% otherwise be needed can be eliminated), and thus,
% once one masters the whole-array concept, to faster
% code development and implementation.  In addition
% manipulation of entire arrays, rather than individual
% elements, can lead to significant enhancement in the 
% efficiency of a MATLAB computation---especially for
% large arrays---since the array operations can 
% be directly executed by "lower level routines",
% typically coded in Fortran, C, or even assembly 
% language.
%
% In the lectures, lab exercises and homeworks to 
% follow, we will often try to emphasize the 
% whole-array approach, not least since it is
% powerful, but it generally takes some getting used to, 
% and requires that one adopt a somewhat different perspective 
% than when using component-wise techniques.  
% Nonetheless, one should bear in mind that, for the most 
% part, what one can do with whole-array operations, 
% one can also accomplish using element-by-
% element computations.  This is especially relevant
% should you wish to implement some of the methods
% and algorithms that we will be discussing in some 
% other programming language that does not provide 
% support for whole-array operations.
%
% With that preamble, let us first note that essentially
all values and variables in MATLAB are represented
% as arrays.  For numerical values, the types of arrays
% that will be of most concern to us are as follows:

% Array Dimension       Name     Typical Array Element
%
%       0              Scalar             a0
%       1            Row Vector         a1r(k)
%       1          Column Vector        a1c(k)
%       2              Matrix           a2(k,p)
%       
% Note the following:
%
%   1) As we will see, MATLAB distinguishes between
%      row and column vectors, concepts which should 
%      be familiar to you from your studies of linear 
%      algebra
%
%   2) Individual array elements are referenced using
%      a single set of the usual parenthesis (), and
%      integer-valued subscripts (indexes), with commas
%      separating the subscripts as necessary.
%
%   3) I will try to deliberately avoid the use of 
%      i and j as subscripts due to their 
%      predefined meaning as sqrt(-1) in MATLAB.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2.1 Creating a One Dimensional Array (Vector)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Creating a vector from a given list of numbers
%
Row vector: General syntax
%
<name> = [ <num1> <num2> ... <numN> ]
%
Column vector: General syntax
%
<name> = [ <num1><num2>; ... ; <numN> ]
%
IMPORTANT! when creating vectors (and this 
% will apply to arrays in general), one uses square
% brackets.

% Also, for row vectors, the individual numbers (array
% elements) that are specified can be separated using
% whitespace (you can also use commas, but they are not
% necessary).

% For column vectors, the semicolons between successive
% elements are crucial if you want to enter the vector
% on one line, otherwise you can omit the semicolons
% but will then have to hit ENTER after each entry,
% i.e. the entries will have to be input one per line.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  yr = [1984 1986 1988 1990 1992 1994 1996]

yr =

   1984   1986   1988   1990   1992   1994   1996


>>  pop = [127; 130; 136; 145; 158; 178; 211]

pop =

   127
   130
   136
   145
   158
   178
   211


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Creating a row vector with constant spacing by 
% specifying the first element, the spacing and the last 
% element

%
% General syntax
%
% 1) <name> = [<first>:<spacing>:<last>]
% 2) <name> =  <first>:<spacing>:<last>
% 3) <name> = [<first>:<last>]
% 4) <name> =  <first>:<last>

% Note that the [ ] are optional, and that if 
<spacing> is omitted, it defaults to 1.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  x = [1:2:13]

x =

    1    3    5    7    9   11   13

% Observe that brackets do not appear in the output

>>  y = [1.5:0.1:2.1]

y =

   1.5000   1.6000   1.7000   1.8000   1.9000   2.0000   2.1000


>>  w = [1:10]

w =

    1    2    3    4    5    6    7    8    9   10


>>  w1 = 1:10

w1 =

    1    2    3    4    5    6    7    8    9   10


>>  z = [-3:7]

z =

  -3  -2  -1   0   1   2   3   4   5   6   7


>>  xa = [21:-3:6]

xa =

   21   18   15   12    9    6


>>  xb = 21:-3:6

xb =

   21   18   15   12    9    6


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Creating a row vector with constant spacing by 
% specifying the first element, the last element and 
% the number, n, of elements (length of the vector)

%
% General syntax
%
% 1) <name> = linspace(<first>,<last>,<n>)
% 2) <name> = linspace(<first>,<last>)

% Note that this uses the MATLAB command (function),
linspace, which computes the necessary 
% spacing between elements.    

% When <n> is omitted, it defaults to 100.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  va = linspace(0, 8, 6)

va =

   0.00000   1.60000   3.20000   4.80000   6.40000   8.00000


>>  vb = linspace(30, 10, 11)

vb =

   30   28   26   24   22   20   18   16   14   12   10


>>  u = linspace(49.5, 0.5)

u =

 Columns 1 through 7:

   49.50000   49.00505   48.51010   48.01515   47.52020   47.02525   46.53030

 Columns 8 through 14:

   46.03535   45.54040   45.04545   44.55051   44.05556   43.56061   43.06566

 Columns 15 through 21:

   42.57071   42.07576   41.58081   41.08586   40.59091   40.09596   39.60101

 Columns 22 through 28:

   39.10606   38.61111   38.11616   37.62121   37.12626   36.63131   36.13636

 Columns 29 through 35:

   35.64141   35.14646   34.65152   34.15657   33.66162   33.16667   32.67172

 Columns 36 through 42:

   32.17677   31.68182   31.18687   30.69192   30.19697   29.70202   29.20707

 Columns 43 through 49:

   28.71212   28.21717   27.72222   27.22727   26.73232   26.23737   25.74242

 Columns 50 through 56:

   25.24747   24.75253   24.25758   23.76263   23.26768   22.77273   22.27778

 Columns 57 through 63:

   21.78283   21.28788   20.79293   20.29798   19.80303   19.30808   18.81313

 Columns 64 through 70:

   18.31818   17.82323   17.32828   16.83333   16.33838   15.84343   15.34848

 Columns 71 through 77:

   14.85354   14.35859   13.86364   13.36869   12.87374   12.37879   11.88384

 Columns 78 through 84:

   11.38889   10.89394   10.39899    9.90404    9.40909    8.91414    8.41919

 Columns 85 through 91:

    7.92424    7.42929    6.93434    6.43939    5.94444    5.44949    4.95455

 Columns 92 through 98:

    4.45960    3.96465    3.46970    2.97475    2.47980    1.98485    1.48990

 Columns 99 and 100:

    0.99495    0.50000


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2.2 Creating a Two Dimensional Array (Matrix)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% First note that when we refer to an m x n ("m by n")
% matrix (or array), we mean a matrix that has 
m rows and n columns, and that "m by n"
% is known as the size of the matrix.
%
% General syntax

<name> = [ <el_11> <el_12> ... <el_1n>;
%            <el_21> <el_22> ... <el_2n>;
%                       ....
%            <el_m1> <el_m2> ... <el_mn> ]

% where the line breaks in the above description are 
NOT necessary.
%
% That is, one specifies the elements of a matrix
% row-by-row, with a semi-colon (or equivalently,
% a new-line [ENTER]) separating the rows.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  a = [5 35 43; 4 76 81; 21 32 40]

a =

    5   35   43
    4   76   81
   21   32   40


>>  cd = 6; e = 3; h = 4;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Note the use of commas to separate entries in a 
% given row.  Again, these are optional.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  Mat = [e, cd*h, cos(pi/3); h^2, sqrt(h*h/cd), 14]

Mat =

    3.00000   24.00000    0.50000
   16.00000    1.63299   14.00000


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Here's an example where the rows of a matrix are 
% generated using the : (colon) notation, or linspace,
% to make vectors with constant spacing between 
% their elements.  One must ensure that each of the 
% row-generating statements returns the same
% number of elements.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  A = [1:2:11; 0:5:25; linspace(10, 60, 6); 67 2 43 68 4 13]

A =

    1    3    5    7    9   11
    0    5   10   15   20   25
   10   20   30   40   50   60
   67    2   43   68    4   13


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB has several built-in commands (functions) to 
% produce matrices with special elements: 
%
% 1) zeros(m,n):  Returns an m x n matrix with 
%                 elements that all 0.
% 2) ones(m,n):   Returns an m x n matrix with 
%                 elements that all 1.
% 3) eye(n):      Creates an n x n (square) 
%                 matrix with 1's along the 
%                 main diagonal, and 0's everywhere
%                 else---i.e. creates the the n x n 
%                 identity matrix
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  zr = zeros(3, 4)

zr =

   0   0   0   0
   0   0   0   0
   0   0   0   0


>>  ne = ones(4, 3)

ne =

   1   1   1
   1   1   1
   1   1   1
   1   1   1


>>  idn = eye(5)

idn =

Diagonal Matrix

   1   0   0   0   0
   0   1   0   0   0
   0   0   1   0   0
   0   0   0   1   0
   0   0   0   0   1



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2.3 Notes about Variables in MATLAB.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1) As noted previously, essentially all variables
%    in MATLAB are arrays.

% 2) The size of a variable is (initially) defined 
%    by the size of the left hand side with which
%    it is initialized.  There is no need to explicitly
%    define the size of any array before its elements
%    are assigned.
%
% 3) Once a variable exists as a scalar, vector, matrix
%    etc., both its type and size can be changed, as 
%    we will see in examples below.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2.4 The Transpose Operator
%
% MATLAB uses a single forward quote character (')
% to denote transpose, with an action as follows

% 1) <row vector>' = <column vector>
% 2) <column vector>' = <row vector>
% 3) <matrix>' = <matrix transpose>
%
% In all cases the elements in the transposed array
% are the same as those in the original array, but,
% for the case of a matrix, they appear in a different 
% order.

% Here it is useful to view a row vector of length
% n as a 1 x n matrix, and a column vector of length
% m as a m x 1 matrix.  We then have

% 1) <1 x n>' = <n x 1>
% 2) <m x 1>' = <1 x m>
% 3) <m x n>' = <n x m>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  aa = [3 8 1]

aa =

   3   8   1


>>  bb = aa'

bb =

   3
   8
   1


>>  C = [2 55 14 8; 21 5 32 11; 41 64 9 1]

C =

    2   55   14    8
   21    5   32   11
   41   64    9    1


>>  D = C'

D =

    2   21   41
   55    5   64
   14   32    9
    8   11    1


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Note that two consecutive applications of transpose
% is an identity operation.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>>  C

C =

    2   55   14    8
   21    5   32   11
   41   64    9    1


>>  C''

ans =

    2   55   14    8
   21    5   32   11
   41   64    9    1


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2.5 Array Addressing 

% (Selecting individual elements from an array)
%
IMPORTANT!: Again note that the usual parentheses 
( ) are used for this purpose.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2.5.1 Vectors 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  vct = [35 36 78 23 5 14 82 3 55]

vct =

   35   36   78   23    5   14   82    3   55


>>  vct(4)

ans =  23

>>  vct(6) = 273

vct =

    35    36    78    23     5   273    82     3    55


>>  vct(2) + vct(8)

ans =  39

>>  vct(5)^vct(8) + sqrt(vct(7))

ans =  134.06

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Can refer to the last element in a vector using the 
% special address/index end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  vct(end)

ans =  55

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2.5.2 Matrices
%
% General syntax:
%
<matrix>(k,p) -> element in row k, column p
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  MAT = [3 11 6 5; 4 7 10 2; 13 9 0 8]

MAT =

    3   11    6    5
    4    7   10    2
   13    9    0    8


>>  MAT(2,1)

ans =  4

>>  MAT(3,3)

ans = 0

>>  MAT(3,1) = 20

MAT =

    3   11    6    5
    4    7   10    2
   20    9    0    8


>>  MAT(2,4) - MAT(1,2)

ans = -9

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The special index/address end can also be 
% used with matrices.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  MAT(1,end)

ans =  5

>>  MAT(end,2)

ans =  9

>>  MAT(end,end)

ans =  8

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2.6 Using a Colon : In Array Addressing.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2.6.1 Vectors

% General syntax:
%
<vector>(:)   = all elements of row or column vector 
<vector>(m:n) = elements m through n of vector
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  v = [4 15 8 12 34 2 50 23 11]

v =

    4   15    8   12   34    2   50   23   11


>>  u = v(3:7)

u =

    8   12   34    2   50


% u(:) is synonym for u (not very useful for vectors!)

>>  u(:)

ans =

    8
   12
   34
    2
   50


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2.6.2 Matrices

% General syntax:
%
<matrix>(:, :)     = all elements of matrix
<matrix>(:, n)     = all elements of column n
<matrix>(m, :)     = all elements of row m
<matrix>(:, m:n)   = all elements between columns m 
%                      and n inclusive
<matrix>(m:n, :)   = all elements between rows m 
%                      and n inclusive
<matrix>(m:n, p:q) = all elements between rows m 
%                      and n, and columns p and q
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  A = [1:2:11; 2:2:12; 3:3:18; 4:4:24; 5:5:30]

A =

    1    3    5    7    9   11
    2    4    6    8   10   12
    3    6    9   12   15   18
    4    8   12   16   20   24
    5   10   15   20   25   30


>>  b = A(:, 3)

b =

    5
    6
    9
   12
   15


>>  c = A(2, :)

c =

    2    4    6    8   10   12


>>  E = A(2:4, :)

E =

    2    4    6    8   10   12
    3    6    9   12   15   18
    4    8   12   16   20   24


>>  F = A(1:3, 2:4)

F =

    3    5    7
    4    6    8
    6    9   12


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2.6a Selecting Elements Using Vectors Constructed
%      With [ ... ] Notation

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  vv = 10:20

vv =

   10   11   12   13   14   15   16   17   18   19   20


>>  vv([2 3 5 9])

ans =

   11   12   14   18


>>  v = 4:3:34

v =

    4    7   10   13   16   19   22   25   28   31   34


>>  [3, 5, 7:10]

ans =

    3    5    7    8    9   10


>>  u = v( [3, 5, 7:10] )

u =

   10   16   22   25   28   31


>>  A = [10:-1:4; ones(1,7); 2:2:14; zeros(1,7)]

A =

   10    9    8    7    6    5    4
    1    1    1    1    1    1    1
    2    4    6    8   10   12   14
    0    0    0    0    0    0    0


% Select rows 1, 3 and 4

>>  A([1, 3, 4], :)

ans =

   10    9    8    7    6    5    4
    2    4    6    8   10   12   14
    0    0    0    0    0    0    0


% Select colums 2, 3 and 6

>>  A(:, [2, 3, 6])

ans =

    9    8    5
    1    1    1
    4    6   12
    0    0    0


% Select columns 1, 3, 5, 6 and 7 from rows 1 and 3

>>  B = A([1, 3], [1, 3, 5:7])

B =

   10    8    6    5    4
    2    6   10   12   14


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2.7 Adding Elements to Existing Variables
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Adding elements to a vector
%
% Elements can be added to an existing vector by 
% assigning values to the new elements.  If the 
% vector is of length n, and the value assigned is 
% for an element with address p which is >= n + 2,
% then elements n + 1, ... p - 1 are implicitly assigned 
% the value 0 (default value).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  df = 1:4

df =

   1   2   3   4


>>  df(5:10) = 10:5:35

df =

    1    2    3    4   10   15   20   25   30   35


>>  ad = [5 7 2]

ad =

   5   7   2


% Increase length of ad to 8 elements ... element 8
% is set to 4, elements 4, 5, 6 and 7 set to 0 (implicitly)

>>  ad(8) = 4

ad =

   5   7   2   0   0   0   0   4


% This assigns length 5 row vector to ar, last element 
% is set to 5, elements 1, 2, 3 and 4 to 0 (implicitly)

>>  ar(5) = 24

ar =

    0    0    0    0   24


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Elements can also be added to a vector by appending
% (concatenating) existing vectors
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  re = [3 8 1 24]

re =

    3    8    1   24


>>  gt = 4:3:16

gt =

    4    7   10   13   16


>>  knh = [re gt]

knh =

    3    8    1   24    4    7   10   13   16


% Recall that ' is the transpose operator:

% row' -> column
% column' -> row

>>  re'

ans =

    3
    8
    1
   24


>>  gt'

ans =

    4
    7
   10
   13
   16


% To concatentate column vectors use ';' between vectors

>>  knv= [re'; gt']

knv =

    3
    8
    1
   24
    4
    7
   10
   13
   16


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Adding elements to a matrix
%
% Rows and/or columns can be added to an existing 
% matrix by assigning values to the new rows or columns.
%
% One must be careful in this case since the size of 
% the added rows/columns must be compatible with the 
% existing matrix.
%
% As was the case for vectors, any matrix elements 
% that are implicitly created by an operation that 
% changes the size of the matrix, and which are not 
% assigned explicit values, are set to 0.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  E = [1 2 3 4; 5 6 7 8]

E =

   1   2   3   4
   5   6   7   8


% Add a third row

>>  E(3,:) = [10:4:22]

E =

    1    2    3    4
    5    6    7    8
   10   14   18   22


>>  K = eye(3)

K =

Diagonal Matrix

   1   0   0
   0   1   0
   0   0   1


% Concatenate E and K: each has 3 rows
% Result has 3 rows and (4 + 3) = 6 columns

>>  G = [E K]

G =

    1    2    3    4    1    0    0
    5    6    7    8    0    1    0
   10   14   18   22    0    0    1


>>  H = G'

H =

    1    5   10
    2    6   14
    3    7   18
    4    8   22
    1    0    0
    0    1    0
    0    0    1


% Concatenate G and K: each has 3 columns
% Result has 3 columns and (7 + 3) = 10 rows

% As with vectors, use ';' to concatenate columns

>>  M = [H; K]

M =

    1    5   10
    2    6   14
    3    7   18
    4    8   22
    1    0    0
    0    1    0
    0    0    1
    1    0    0
    0    1    0
    0    0    1


>>  AW = [3 6 9; 8 5 11]

AW =

    3    6    9
    8    5   11


% This adds 2 rows and 3 cols to AW, new elements 
% that are not explictly set are assigned 0

>>  AW(4,5) = 17

AW =

    3    6    9    0    0
    8    5   11    0    0
    0    0    0    0    0
    0    0    0    0   17


% This assigns 3 x 4 array to BG, sets "corner" element
% to 15, rest to 0

>>  BG(3,4) = 15

BG =

    0    0    0    0
    0    0    0    0
    0    0    0   15


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2.8 Deleting Elements
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% An element, or range of elements, can be deleted 
% by assigning "nothing", denoted syntactically by
[], to it/them.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  kt = [2 8 40 65 3 55 23 15 75 80]

kt =

    2    8   40   65    3   55   23   15   75   80


% Delete the 6th element: note that this modifies kt

>>  kt(6) = []

kt =

    2    8   40   65    3   23   15   75   80


% Delete elements 3, 4, 5 and 6 of (the modified) kt

>>  kt(3:6) = []

kt =

    2    8   15   75   80


>>  mtr = [5 78 4 24 9; 4 0 36 30 12; 56 13 5 89 3]

mtr =

    5   78    4   24    9
    4    0   36   30   12
   56   13    5   89    3


% Delete columns 2, 3 and 4 

>>  mtr(:,2:4) = []

mtr =

    5    9
    4   12
   56    3


% What happens if we try to delete one element of a 
% matrix?  Error?

% mtr(1:1) = []

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2.9 Built-in Functions for Handling Arrays
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB has a rich set of functions for managing and
% manipulating arrays.  Some of these are as follows:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
length(<vector>
%
%    Returns the number of elements in <vector>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  a = [5 9 2 4]

a =

   5   9   2   4


>>  length(a)

ans =  4

>>  length(linspace(0, 1, 10001))

ans =  10001


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
size(<matrix>
%
%    If <matrix> is m x n, then returns the row
%    vector [m n]
%
% i.e. size returns the "dimensions" of a matrix,
% or of an array in general
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  A = [6 1 4 0 12; 5 19 6 8 2]

A =

    6    1    4    0   12
    5   19    6    8    2


>>  size(A)

ans =

   2   5


% A row vector of length n is viewed as a 1 x n matrix
% A column vector of length m is viewed as a m x 1 matrix

>>  size([1:100])

ans =

     1   100


>>  size([1:200]')

ans =

   200     1


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
reshape(<matrix>, m, n)
%
%    If <matrix> is p x q, then returns a new matrix
%    which is m x n and which contains the same elements
%    as <matrix>, but in an order which is best 
%    illustrated by example, as below.
%
%    Note: m * n must be equal to p * q
%
% This function is a little hard to master, but work
% doing so if you want to become proficient in Matlab!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  A = [5 1 6; 8 0 2]

A =

   5   1   6
   8   0   2


>>  B = reshape(A, 3, 2)

B =

   5   0
   8   6
   1   2


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
diag(<vector>
%
%    If <vector> is of length n, returns an n x n 
%    matrix with the elements of <vector> along the 
%    diagonal, and zeros elsewhere.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  v = [7 4 2]

v =

   7   4   2


>>  A = diag(v)

A =

Diagonal Matrix

   7   0   0
   0   4   0
   0   0   2


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
diag(<matrix>
%
% If <matrix> is m x n, then returns a vector of 
% length m whose elements are the main diagonal of
<matrix>(:, 1:m)
%
% If <matrix> is m x m (i.e. square), this is simply
% the main diagonal of the matrix.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  A = [1 3 4; 4 5 6; 7 8 9]

A =

   1   3   4
   4   5   6
   7   8   9


>>  vec = diag(A)

vec =

   1
   5
   9


>>  B = [1 3 4; 4 5 6]

B =

   1   3   4
   4   5   6


>>  vecB = diag(B)

vecB =

   1
   5


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2.10 Strings and Strings as Variables
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1) In MATLAB, a string is an array (vector) of 
%    characters.
%
% 2) Strings can be created by enclosing an arbitrary 
%    sequence of characters (other than a single forward
%    quote), including whitespace and special characters,
%    within a pair of single (forward) quotes ' '
%
% 3) If you want to include a single quote within a 
%    string, type two consecutive quotes. 
%
% 4) Strings can be assigned to variables using the
%    usual syntax for variable assignment.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  s = 'foo'

s = foo

>>  a = 'FRty 8'

a = FRty 8

>>  B = 'My name is Matt'

B = My name is Matt

>>  c = 'That''s ridiculous!!'

c = That's ridiculous!!

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Since a string is a vector of characters, individual 
% characters in a string, or consecutive sets of 
% characters (substrings), can be extracted and/or 
% assigned values using the standard addressing 
% operations.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  B(4)

ans = n

>>  B(12)

ans = M

>>  c(8:17) = 'capricious'

c = That's capricious!!