Structures, Strings, and Systems
Opening Discussion
■ What did we talk about last class?
■ Do you have any questions about the reading?
■ Do you have any questions about the
assignment?
Structures
■ You can use the dot notation to put fields into a
variable to make a structure. Unlike normal
imperative languages, the format of the structure
isn' t predefined .
■ Matlab deals with arrays of structures just like
numeric arrays .
■ The struct function can build arrays of structures
from existing cell arrays.
■ You can pull out all the values of certain field with
' dynamic addressing '.
Strings
■ Like most other languages, Matlab does give you
the ability to use strings, though that isn't a real
strength.
■ A Matlab string is simply a row array of characters.
■ A downside of this is that an array with multiple
strings must have all the strings be the same
length. The char function can help with that.
■ You can also convert from numbers to strings and
back with str2num and num2str.
■ Matlab also has fprintf and sprintf functions that
work much like the C functions.
■ Similarly, sscanf will pull numbers out of strings .
■ eval and evalc let you process a string like it were
a Matlab function.
Files
■ Save and load commands let you access native
files that store Matlab variables.
■ Doing help fileformats will also show you all the
other formats the Matlab supports normally.
■ You can do low level I/O with C-like functions.
fopen, fclose, fread, fwrite, fscanf, fprintf, etc.
■ In addition, Matlab will also let you play with
directories and even has built in support for ftp if
you want to pull things across a network in Matlab.
■ Let's populate an array with a bunch of values,
write it out in a text file, then read it back in.
Matrix Algebra
■ Now we get into the things that Matlab was really
developed for and where it really stands out.
■ 2-D arrays are basically matrices and can be used
for doing all types of math .
■ Before we get into this we should talk about
systems of linear equations and how they can be
solved.
■ Matlab actually has routines that will try to solve
systems of equations that aren't “well behaved”.
That is, it will approximate both over-determined
and under-determined systems.
Matrix Algebra
■ Now we get into the things that Matlab was really
developed for and where it really stands out.
■ 2-D arrays are basically matrices and can be used
for doing all types of math.
■ Before we get into this we should talk about
systems of linear equations and how they can be
solved.
■ A set of linear equations is typically expressed as
Ax=y, where A is an n by n matrix and x and y are
n by 1 matrices. You are given A and y and want
to solve for x .
■ If the equations are “well behaved” there is a
single solution x =A-1y. Use \ instead of inv().
Overdetermined and
Underdetermined
■ If A isn't square there isn't a single solution. When
A has more rows than columns there is no exact
solution (overdetermined). When A has more
columns than rows there are an infinite number of
solutions (underdetermined).
■ Matlab actually has routines that will try to solve
systems of equations that aren't “well behaved”.
That is, it will approximate both over-determined
and under-determined systems.
■ In the overdetermined case, A\y will give you the
least squares solution. This is can be viewed as
an optimal fit.
Sparse Matrices
■ Matlab also has hat ability to store sparse
matrices.
■ We aren't really going to take advantage of this in
this class, but if you have a large matrix has has
mostly zeros in it , this can be significant .
Closing Comments
■ Assignment #3 is due on Monday and quiz #2 is
on Wednesday.