%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% From: MATLAB An Introduction with Applications
%       Amos Gilat
%
Chapter 3   Mathematical Operations with Arrays
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% There are two ways that the basic binary mathematical 
% operations 

%    +      Addition
%    -      Subtraction
%    *      Multiplication
%    /      Division (right division)
%    \      Left division
%    ^      Exponentiation
%
% can be applied to arrays in MATLAB / octave.
%
% 1) "Whole array" operations, specifically matrix 
%    operations, wherein the above operators follow
%    the usual rules of linear algebra.

% 2) Element-by-element operations, in which case
%    one of the following must be true
%    
%    i)   Both of the operands are scalars.
%
%    ii)  One of the operands is a scalar and the other
%         is an array.
%
%    iii) Both operands are arrays and have the same
%         size.

%    Element-by-element operators are constructed from
%    the usual arithmetic operators by prefixing a '.'
%
%    .*     Element-by-element multiplication
%    ./     Element-by-element (right) division
%    .\     Element-by-element left division
%    .^     Element-by-element exponentiation.
%
%    The result of an element-by-element array operation
%    has the same size of the operand array(s), with
%    values that, as the nomenclature suggests, are 
%    calculated from the action of the particular 
%    binary operator (*, /, \, ^) on corresponding 
%    elements of the input arrays.
%
%    If this is confusing, the examples below should
%    clear things up quickly.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3.1 Addition and Subtraction
%
NOTE: For addition and subtraction, matrix
% operation is identical to element-by-element operation.
%
% If (non-scalar) arrays are added or subtracted then:

%   1) They must have identical sizes.
%
%   2) The arrays are added / subtracted element-wise 
%      to produce the result.
%
% If a scalar and an array are involved in an 
% addition/subtraction operation, the scalar is 
% added/subtracted from all elements of the array.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>>  VectA = [8 5 4]

VectA =

   8   5   4


>>  VectB = [10 2 7]

VectB =

   10    2    7


>>  VectC = VectA + VectB

VectC =

   18    7   11


>>  VectA + 13

ans =

   21   18   17


>>  A = [5 -3 8; 9 2 10]

A =

    5   -3    8
    9    2   10


>>  B = [10 7 4; -11 15 1]

B =

   10    7    4
  -11   15    1


>>  A - B

ans =

   -5  -10    4
   20  -13    9


>>  C = A + B

C =

   15    4   12
   -2   17   11


>>  C - 8

ans =

    7   -4    4
  -10    9    3


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3.2 Array (Matrix) Multiplication>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matrix multiplication in MATLAB is denoted by *
% and is defined according to the standard rule of 
% linear algebra: namely 
%
% If 
%     A is an m x n matrix with elements a(i,j)
%
% and 
%     B is an n x p matrix with elements b(j,k)
%
% then
%     C = A * B is an m x p matrix with elements c(i,k)
%
% defined by  
%               n
%     c(i,k) = Sum [ a(i,j) x b(j,k) ]
%              j=1
%
NOTE: For this operation to be well defined,
% the number of columns of A must equal the number of 
% rows of B.

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

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

A =

   1   4   2
   5   7   3
   9   1   6
   4   2   8


>>  B = [6 1; 2 5; 7 3]

B =

   6   1
   2   5
   7   3


>>  C = A * B

C =

   28   27
   65   49
   98   32
   84   38


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% D = B * A  

% would generate an error message since the number of 
% columns of B is not equal to the number of rows of A
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  F = [1 3; 5 7]

F =

   1   3
   5   7


>>  G = [4 2; 1 6]

G =

   4   2
   1   6


>>  F * G

ans =

    7   20
   27   52


>>  G * F

ans =

   14   26
   31   45


>>  AV = [2 5 1]

AV =

   2   5   1


>>  BV = [3; 1; 4]

BV =

   3
   1
   4


>>  AV * BV

ans =  15

>>  BV * AV

ans =

    6   15    3
    2    5    1
    8   20    4


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3.3 Array (Matrix) Division
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matrix division in MATLAB is also associated with
% the rules of linear algebra; and specifically with 
% the solution of matrix equations.  We thus first 
% review some pertinent definitions and concepts.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
Identity Matrix
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% The identity matrix is a square matrix in which the
% elements along the main diagonal are 1, and all other
% elements are 0.  As we have already seen, the n x n
% identity matrix can be generated in MATLAB using 
% the eye command.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

>>  I = eye(3)

I =

   1   0   0
   0   1   0
   0   0   1


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% In linear algebra, the identity matrix is to general 
% matrices and vectors as the constant 1 is to real
% numbers, i.e. for any n x m array, A, multiplication
% on the left by the n x n identity matrix I yields
% the original array, A, that is 
%
%  I A = A
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

>>  A = [1 2; 3 4; 5 6]

A =

   1   2
   3   4
   5   6


>>  I * A

ans =

   1   2
   3   4
   5   6


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% In the special case that A is n x n (i.e. square),
% we have

I A = A I = A
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

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

A =

   1   2   3
   4   5   6
   7   8   9


>>  I * A

ans =

   1   2   3
   4   5   6
   7   8   9


>>  A * I

ans =

   1   2   3
   4   5   6
   7   8   9


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
Inverse of a Matrix
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%
% If A is a n x n (square) matrix, then the n x n (square)
% matrix B is the inverse of A if and only if
%
A * B = B * A = I

% where I is the n x n identity matrix.

%                                    -1
% The inverse of A is often denoted A   and can be 
% computed in MATLAB either by raising A to the -1
% power, or by using the inv function.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

>>  A = [2 1 4; 4 1 8; 2 -1 3]

A =

   2   1   4
   4   1   8
   2  -1   3


>>  B = inv(A)

B =

   5.50000  -3.50000   2.00000
   2.00000  -1.00000   0.00000
  -3.00000   2.00000  -1.00000


>>  A * B

ans =

   1   0   0
   0   1   0
   0   0   1


>>  B * A

ans =

   1   0   0
   0   1   0
   0   0   1


>>  A^-1

ans =

   5.50000  -3.50000   2.00000
   2.00000  -1.00000   0.00000
  -3.00000   2.00000  -1.00000


>>  A * A^-1

ans =

   1   0   0
   0   1   0
   0   0   1


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Not all square matrices have inverses.  Indeed, 
% the necessary and sufficient condition for the n x n
% matrix A to have an inverse is that its determinant
% be non-vanishing.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Determinants
%
% I assume that you have seen the definition of the 
% determinant of a matrix, if not, refer to any text
% on linear algebra (or wikipedia if you must :-)).  
% In MATLAB, the determinant of a (square) matrix can 
% be computed using the det function.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  A = [1 2; 3 4]

A =

   1   2
   3   4


>>  det(A)

ans = -2

>>  A = [3 4; 1 2]

A =

   3   4
   1   2


>>  det(A)

ans =  2

>>  A = [4 3; 2 1]

A =

   4   3
   2   1


>>  det(A)

ans = -2

>>  A = [2 1; 4 3]

A =

   2   1
   4   3


>>  det(A)

ans =  2

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Array (Matrix) Division

% MATLAB has 2 types of matrix division, namely
% right and left division.  Both are best defined
% in the context of solving matrix equations.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Left Division \
%
% Left division is used to solve the matrix equation

A X = B
%
% where A is an n x n matrix, while X and B are column
vectors of length n.

% This equation can be solved by left-multiplying both 
% sides with the inverse of A:
%
%  -1       -1
% A  A X = A  B

%           -1 
%    I X = A  B
%
%           -1
%      X = A  B
%
% This last expression can be defined in terms of the 
left division operator \ as
%
X = A \ B
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Right Division \
%
% Right division is used to solve the matrix equation

%  X C = D
%
% where C is an n x n matrix, while X and D are row
vectors of length n.

% This equation can be solved by right-multiplying both 
% sides with the inverse of C
%
%       -1     -1
%  X C C  = D C
%
%              -1 
%     X I = D C
%
%              -1
%       X = D C
%
% This last expression can be defined in terms of the 
right division operator / as
%
X = D / C
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Example: Solving three linear equations
%
% Use matrix operations to solve the following system
% of linear equations
%
%    4x -  2y + 6z = 8
%    2x +  8y + 2z = 4
%    6x + 10y + 3z = 0
%
%  This can be solved in either of the following forms

%  -            -  -   -   -   -
%  |  4  -2   6 |  | x |   | 8 |
%  |  2   8   2 |  | y | = | 4 |
%  |  6  10   3 |  | z |   | 0 |
%  -            -  -   -   -   -
%
%               -            -         
%  -         -  |  4   2   6 |    -         -
%  | x  y  z |  | -2   8  10 |  = | 8  4  0 |
%  -         -  |  6   2   3 |    -         - 
%               -            -          
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  A = [4 -2 6; 2 8 2; 6 10 3]

A =

    4   -2    6
    2    8    2
    6   10    3


>>  B = [8; 4; 0]

B =

   8
   4
   0


>>  X = A \ B

X =

  -1.80488
   0.29268
   2.63415


>>  Xb = inv(A) * B

Xb =

  -1.80488
   0.29268
   2.63415


>>  C = [4 2 6; -2 8 10; 6 2 3]

C =

    4    2    6
   -2    8   10
    6    2    3


>>  D = [8 4 0]

D =

   8   4   0


>>  Xc = D / C

Xc =

  -1.80488   0.29268   2.63415


>>  Xd = D * inv(C)

Xd =

  -1.80488   0.29268   2.63415


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3.4 Element-By-Element Operations
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% As already noted above, the following binary operators
%
%   *      multiplication
%   \      right division
%   /      left division
%   ^      exponentiation
%
% can be applied to arrays in an element-by-element
% fashion by prefixing the operator with a '.'
%
%   .*     element-by-element multiplication
%   .\     element-by-element right division
%   ./     element-by-element left division
%   .^     element-by-element exponentiation

NOTE: When using element-by-element operations
% between arrays, the arrays must be the same size.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  A = [2 6 3; 5 8 4]

A =

   2   6   3
   5   8   4


>>  B = [1 4 10; 3 2 7]

B =

    1    4   10
    3    2    7


>>  A .* B

ans =

    2   24   30
   15   16   28


>>  C = A ./ B

C =

   2.00000   1.50000   0.30000
   1.66667   4.00000   0.57143


>>  D = A .\ B

D =

   0.50000   0.66667   3.33333
   0.60000   0.25000   1.75000


>>  C .* D

ans =

   1   1   1
   1   1   1


>>  B .^ 3

ans =

      1     64   1000
     27      8    343


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Using element-by-element operations to compute a
% function at many values of its argument.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  x = [1:8]

x =

   1   2   3   4   5   6   7   8


>>  y = x.^2 - 4*x

y =

   -3   -4   -3    0    5   12   21   32


>>  z = [1:2:11]

z =

    1    3    5    7    9   11


>>  y = (z.^3 + 5*z) ./ (4*z.^2 - 10)

y =

  -1.0000   1.6154   1.6667   2.0323   2.4650   2.9241


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3.5 Using Arrays In MATLAB Built-in Math Functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% When any of MATLAB's built in math functions is 
% invoked with an array argument, the output is an
% array with identical size of the input, and with 
% elements whose values are the results of applying
% the function to the corresponding elements of 
% the input array.

% In other words, "what you get is what you expect"!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  x = [0:pi/6:pi]

x =

   0.00000   0.52360   1.04720   1.57080   2.09440   2.61799   3.14159


>>  y = cos(x)

y =

 Columns 1 through 6:

   1.0000e+00   8.6603e-01   5.0000e-01   6.1230e-17  -5.0000e-01  -8.6603e-01

 Column 7:

  -1.0000e+00


>>  d = [1 4 9; 16 25 36; 49 64 81]

d =

    1    4    9
   16   25   36
   49   64   81


>>  h = sqrt(d)

h =

   1   2   3
   4   5   6
   7   8   9


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3.6 Built In Functions for Analyzing Arrays
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Reduction Functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Many of the functions described below (those marked
% with an (*)) operate on arrays of general dimensions,
% as well as on vectors.  Furthermore, when operating 
% on a vector, many of these functions return a 
single value.
%
% For higher dimensional arrays, and for matrices in
% particular, such functions generally operate along
% columns
 (i.e. along the first dimension of the 
% array), returning a row vector of values: each value
% in the row vector is the result of applying the 
% function to the elements of the corresponding 
% column of the original matrix.

% One net effect of these functions is thus to reduce 
% the dimensionality of the array by one (or, more 
% properly, to collapse the length of the first 
% dimension to 1): such functions are therefore often 
% termed reductions.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% max(<A>) (*)

%   If <A> is a vector, returns the largest element in 
%   <A>.
%
%   If <A> is a matrix, returns a row vector containing 
%   the largest element of each column of <A>.
%
% [<d><n>] = max(<A>) (*)

%   If <A> is a vector, <d> is the largest element
%   in <A> and <n> is the (first) position of the 
%   largest element.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Hereafter, the result of applying a reduction 
% function to a matrix will not be explicitly stated, 
% and the stated return value(s) will be those for 
% a vector argument.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  A = [5 9 2 4 11 6 11 1]

A =

    5    9    2    4   11    6   11    1


>>  C = max(A)

C =  11

>>  [d, n] = max(A)

d =  11
n =  5

>>  AA = [1 3 2; 4 1 3; 8 11 12; -2, 6, 100]

AA =

     1     3     2
     4     1     3
     8    11    12
    -2     6   100


>>  CC = max(AA)

CC =

     8    11   100


>>  [dd, nn] = max(AA)

dd =

     8    11   100

nn =

   3   3   4


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% min(<A>) (*)

%   Returns the smallest element in <A>.
%
% [<d><n>] = min(<A>) (*)

%   <d> is the smallest element in <A> and <n> 
%   is the (first) position of the smallest element.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  A = [5 9 2 4 11 6 11 1]

A =

    5    9    2    4   11    6   11    1


>>  C = min(A)

C =  1

>>  [d, n] = min(A)

d =  1
n =  8

>>  AA = [1 3 2; 4 1 3; 8 11 12; -2, 6, 100]

AA =

     1     3     2
     4     1     3
     8    11    12
    -2     6   100


>>  CC = min(AA)

CC =

  -2   1   2


>>  [dd, nn] = min(AA)

dd =

  -2   1   2

nn =

   4   2   1


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% sum(<A>) (*)

%   Returns the sum of elements of <A>.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  A = [5 9 2 4]

A =

   5   9   2   4


>>  sum(A)

ans =  20

>>  AA = [1 3 2; 4 1 3; 8 11 12; -2, 6, 100]

AA =

     1     3     2
     4     1     3
     8    11    12
    -2     6   100


>>  sum(AA)

ans =

    11    21   117


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Statistical Functions (assuming <A> is a vector)
%
% (These are all reductions.)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% mean(<A>) (*)
%
%   Returns the mean value of elements of <A>.
%
% median(<A>) (*)
%
%   Returns the median value of elements of <A>.
%
% std(<A>) (*)
%
%   Returns the standard deviation of elements of <A>.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Miscellaneous Functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% sort(<A>) (*)

%   Returns the elements of <A> sorted in ascending 
%   order.
%
NOTE: sort is not a reduction.  It 
% returns an array of the same size as its input.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  A = [5 9 2 4]

A =

   5   9   2   4


>>  sort(A)

ans =

   2   4   5   9


>>  AA = [1 3 2; 4 1 3; 8 11 12; -2, 6, 100]

AA =

     1     3     2
     4     1     3
     8    11    12
    -2     6   100


>>  sort(AA)

ans =

    -2     1     2
     1     3     3
     4     6    12
     8    11   100


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% det(<A>

%   Returns the determinant of a square matrix <A>.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  A = [2 4; 3 5]

A =

   2   4
   3   5


>>  detA = det(A)

detA = -2.0000

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% inv(<A>

%   Returns the inverse of a square matrix <A>.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  A = [2 4; 3 5]

A =

   2   4
   3   5


>>  invA = inv(A)

invA =

  -2.50000   2.00000
   1.50000  -1.00000


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% dot(<a>,<b>

%   Returns the scalar (dot) product of two vectors <a>
%   and <b>, which can be either row or column 
%   vectors, but which must be the same length.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% cross(<a>,<b>)
%
%   Calculates the cross product of two vectors <a> and 
%   <b>, each of which must be of length 3.  Returns
%   a vector which also has 3 elements.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3.7 Generation of (Pseudo)-Random Numbers
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
NOTE: As with many languages and software 
% systems, the "random" numbers generated by MATLAB
% are actually computed using specific, deterministic
% algorithms, and are thus not truly random: hence
% the nomenclature "pseudo-random".
% However, "good" random number generators, such as 
% those used by MATLAB / octave are designed so that 
% the sequences of numbers produced share many 
% statistical properties with true random values.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% rand
%
%   Generates a single pseudo-random number, drawn from 
%   a uniform distribution between 0 and 1 (i.e. the 
%   probability of getting any number in the range is 
%   the same).  Also note that the generator will never
%   produce exactly 0 or exactly 1.  We will refer to
%   such a value as a "standard random number" 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  rand

ans =  0.50311

>>  for ir = 1:10
rand
end

ans =  0.93001
ans =  0.85691
ans =  0.72454
ans =  0.84821
ans =  0.59074
ans =  0.10582
ans =  0.15985
ans =  0.25440
ans =  0.98607
ans =  0.11381

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% rand(1,n)
%
%   Generates an n element row vector of standard 
%   random numbers.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  a = rand(1,4)

a =

   0.82390   0.19355   0.83886   0.57569


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% rand(n)
%
%   Generates an n x n matrix of standard random 
%   numbers.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  b = rand(3)

b =

   0.10873   0.33764   0.15473
   0.15919   0.85946   0.49726
   0.96393   0.17165   0.13604


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% rand(m, n)
%
%   Generates an m x n matrix of standard random 
%   numbers.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  c = rand(2, 4)

c =

   0.302023   0.113306   0.959408   0.050216
   0.743575   0.222893   0.998780   0.191937


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% randperm(n)
%
%   Generates a n element row vector whose values are
%   a random permutation of the integers 1 through n.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Changing/resetting the random number sequence
%
% One easy way to tell that the generation of random 
% numbers in MATLAB isn't really random is to note that 
% every time you start the program, the first 
% invocation of rand will return the same 
% number (0.8147 on the current version). Indeed.
% the entire sequence of numbers that will be 
% generated by successive calls to rand will,
% by default, be the same from session to session.

% octave, on the other hand, doesn't behave this way:
% the value that is returned the first that time rand 
% is called is session-dependent, as is the sequence
% of numbers returned by subsequent calls.

% At times, especially when one is developing programs
% that use random numbers, it can be convenient to 
% ensure that the sequence is the same from run to run.

% This can be done by "seeding" the random number 
% generator, which essentially sets the "state" of 
% the random number algorithm to a specific 
% configuration that uniquely depends on the so-called
% "seed" value that is supplied to the generator,
% but which is otherwise a relatively unpredictable
% function of the seed.

% In both MATLAB and octave one can seed the random
% number generator using.
%
% rand('seed',<seed>)

% where <seed> is an essentially arbitrary value.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  rand('seed', 0.1)

>>  vr = rand(1,5)

vr =

   0.623141   0.388699   0.536257   0.013492   0.190336


>>  rand

ans =  0.88393

>>  rand('seed', 0.1)

>>  vr = rand(1,5)

vr =

   0.623141   0.388699   0.536257   0.013492   0.190336


>>  rand

ans =  0.88393

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Generating Uniformly Distributed Pseudo-Random 
% Numbers on an Arbitrary Range

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% To generate pseudo-random numbers uniformly 
% distributed on the interval (a,b) use

% a + (b - a) * rand 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Example: Generate 10 random numbers on the interval
% (-1,1)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  vr = -1 + 2 * rand(1,10)

vr =

 Columns 1 through 8:

   0.92739   0.99257   0.84525  -0.13459   0.14655   0.64656  -0.57485   0.79187

 Columns 9 and 10:

   0.49449  -0.24469


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Generating Uniformly Distributed Integers
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% To generate integers uniformly distributed from m to
% n use

% fix(m + (n - m) * rand)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Example: Generate 10 random integers between 1 and 3.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  vr = fix(1 + 3 * rand(1,10))

vr =

   1   1   2   3   2   2   2   3   2   3


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Normally distributed pseudo-random numbers
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Both MATLAB and octave have a function randn which
% operates analogously to rand, but which generates 
% pseudo-random numbers that are normally distributed 
% with zero mean and unit standard deviation.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>>  vrn = randn(1,20)

vrn =

 Columns 1 through 7:

  -1.968572  -1.923352   1.196473  -1.086436   0.376728   0.053893  -0.596372

 Columns 8 through 14:

   1.503941  -0.198518   0.177714   1.526630   0.729400   0.639732  -0.680810

 Columns 15 through 20:

   0.413040   3.672601   0.460874  -1.493436   0.636787  -0.428483