 |
We have already seen vectors and matrices in the Introductory MATLAB class.
As you probably remember, matrices are the elementary data structure in
MATLAB. Also matrices are sometime called arrays. This is because they
are used outside their linear algebra context, just as tables of numbers.
For example an array can be a way of storing an image (with a "brightness"
value in each entry), whereas a matrix represents a linear transformation.
Some operations on arrays are matrix operations: examples are the sum
of two matrices, the product of two matrices or the inverse of a matrix.
Thinking of the example above: it does make sense to sum or multiply (compose)
two different linear transformations or to invert one linear transformation
but operating this way on a picture doesn't make much sense.
Important: Please before proceeding with
these notes download and run the M-file arrays.m,
it contains all the data that are going to be used in the examples of this
section. For the "rusty" ones you run an M-file by
simply typing its name without the suffix. In this case you should
enter
arrays
Linear Algebra
This is when we think of arrays as matrices. Given two matrices A
and B, and assuming they satisfy the suitable compatibility conditions,
we can calculate very easily A+B, AB, or AT.
Also given a vector x and a (non singular) square matrix A we can evaluate
the solution of the linear system Ax=b by typing in
A
b
to check that A and b are defined and then
x = A\b
to solve the linear system.
A related MATLAB function is rref: to solve the system Ax=b
one can alternatively use the reduced echelon form (a "classic" in linear
algebra courses). To do this we type in
augmented = [A,b]
reduced = rref(augmented)
Observe the use of the concatenation to construct the augmented
matrix. To appreciate the power of the concatenation try the following
command
B = [A,A';([b;2*b])']
We try to solve now the underdetermined linear problem By=c (we cannot
use the \ operation here):
c
rref([B,c])
(The set of solutions is then given by the parametric equations
y1 = -0.0238 t + 4.0238 s -
0.5714,
y2 = -2.7143 t - 4.2857 s -1.1429,
y3 = 1.3929 t + 2.1071 s
+ 0.4286,
y4 = -0.3452 t - 4.1548 s + 1.7143,
y5 = t,
y6 = s. )
You have seen (in the Introduction to MATLAB, for example) other functions
that operate on matrices for example: det, inv or
eig.
Another new example of a matrix function is the characteristic polynomial
function of a square matrix, namely poly:
p = poly(A)
will give the coefficients (vector) of the third degree polynomial t
-> det (A - tId) which is
p(t) = t3 - 6 t2
- 56 t - 43.
Arrays
As we mentioned above, arrays are more general than matrices: a pictorial
information is handily stored into an array (which is abusively called
matrix by many, but we will not do so here). Actually, a more suitable
mathematical object to represent a picture is a function, as opposed to
a matrix (because pictures in reality have infinite resolution). But MATLAB
cannot handle infinite sets and a mathematical function (in the calculus
sense, not in the MATLAB sense) is approximated by samples (the arrays!)
which are the values of the function in a finite number of points. So functions
of one real variable are represented in MATLAB by vectors and functions
of two real variables or of one complex variable are represented by doubly
indexed arrays.
What about functions of 3 or more variables? They are too represented
by 3 or more dimensional arrays. For example the temperature in this room
can be represented by a 3 dimensional array: for each point with coordinates
(i,j,k) we have a given temperature T(i,j,k).
Let us have a look:
T
the output is practically a sequence of two dimensional arrays. In this
example we think of the index i as being the physical height. If
we want to take a section of the temperatures at height i = 3, then
we enter
section(:,:) = T(3,:,:)
this will extract a two dimentional array.
We can perform on arrays all the array like operations. The most simple
ones being summarized in the following table:
| operation |
syntax |
| (array) sum |
+ |
| (array) difference |
- |
| array product |
.* |
| array division |
./ |
| array power |
.^ |
Notice that the first two operations don't need the "dot" notation since
their effect is the same whether the array is thought as a function or
as a matrix.
Also scalar functions can be applied to arrays, they act in an entrywise
way. For example
T_rounded = round(10*T)/10
will round the temperature matrix to the tenth of degree. Notice that the
multiplication and the division here don't need the "dot". Why is this?
Vector functions such as sum, max or mean,
can also be applied to arrays, in this case one can give an integer as
an extra parameter. This integer specifies with respect to which index
to perform the given vector function. For instance, suppose we want to
compute the average temperature along the length in the room, then we have
to enter
T_average = mean(T,3)
The output is a two dimensional array such that T_average(i,j) == mean(T(i,j,:))
If we average again in the second index we get
T_average = mean(T_average,2)
which is a vector that represents the average temperature of the room at
different heights.
Accessing
Entries in Arrays through Logical Conditions
We have already seen, in Introductory MATLAB, how to access the entries
of a matrix. Now we examine a new way od accessing entries: by logical
conditions. For example consider the vector
observed_data
and suppose that, for some reason, we would like to get rid of the non-positive
values. Then we can enter
positive_data = observed_data(observed_data>0)
The general syntax is M(L) where M is an array and L
is an array with logical entries and of the same size as M. Notice
that L could be generated from M itself.
This method proves useful when one wants to get rid of spurious results.
Assume for instance that we are given
w
and that we take
w = 1./w
we will produce some infinities. A fast way of getting rid of these entries
is
v = w(finite(w))
Alternatively, one might want to conserve the number entries but set the
spurious one to zero value:
w(~finite(w)) = 0
Another useful command to access entries of a matrix is find.
Its argument is a logical array and the output is a vector of integers
corresponding to the TRUE entries. (Notice that arrays,
no matter what dimension they have, are actually stored as one dimensional
vector. For example a 3x4 matrix is stored in a vector of length 12.)
Example:
A = magic(4)
k = find(isprime(A))'
A(k) = 1
A
Cell Arrays (Sets)
Up to now arrays contained numbers or logical values. There is a more general
structure, called cell array whose elements themselves can be arrays.
The mathematical closest equivalent of cell array is that of set. The elements
of a cell array are enclosed into curly braces {}.
For example, let
B = hilb(5)
S = {B eig(B) det(B)}
Then to retrieve the elements of S we use again the curly braces and access
rules as for arrays.
S{1}
S{2}
Note that we have to use the curly braces, parentheses might lead to weird
results here.
Text and Strings
A string in MATLAB is a vector with character values (integers between
0 and 255). When the characters are entered directly we must enclose them
in quotation marks. Notice that the string itself is just a numerical vector.
Example:
s = 'Peer'
d = round(s)
dd = char(d)
The function char translates integers into ASCII characters, round
(or double, for instance) does the the opposite job, it maps ASCII
characters into numbers.
Strings can be concatenated as arrays, and if you wish to have an array
of strings then you must use the cell array structure:
S = [s,' ','training']
Dictionary = {'Parrot', 'Green bird'; 'Koala', 'Brown mammal'}
Back to top of section.
Back to contents.
|