Other Data Structures
%
5 =
name: 'Ed Plum'
score: 83
grade: 'B+'
Like everything else in MATLAB, structures are arrays. so you can insert
additional elements. In this case. each element ofthe array i
Flow Control
It is important to understand how relational operators and ifstatements work
with matrices. When you want to check for equality between two variables. you
might use
if A = , .
This is leg
Scripts and Functions
%
The example of the previous section could also be done with the following code,
although this would be somewhat less efficient because it involves the full
interpreter, notjust
6 Programming with MATLAB
S
Internally, the characters are stored as numbers, but not in floatingpoint
format. The statement
a = double(s)
converts the character array to a numeric matrix containing
6 Programming with MATLAB
%
Flow Control
MATLAB has several flow control constructs:
 if statements
 switch statements
 for loops
 whi le loops
 continue statements
 break statements
For More In
Scripts and Functions
%
You can see the file with
type rank
Here is the file.
fUnction r = rank(A,tol)
% RANK Matrix rank.
% RAHK(A) provides an estimate of the number of linearly
% independent rows o
Programming with
MATLAB
%
Flow Control . 62
Other Data Structures . 67
Scripts and Functions . 617
Demonstration Programs Included with MATLAB . . . 627
6 Programming with MATLAB
%
S = char('A','rolIing','stone','gathers','momentum.')
produces a 5by9 character array.
5 =
A
rolling
stone
gathers
momentum.
There are enough blanks in each ofthe first f
6 Programming with MATLAB
2
produces a 1by3 cell array. The three cells contain the magic square, the row
vector of column sums. and the product ofall its elements. When C is displayed.
you see
C =
Flow Control
%
It is a good idea to indent the loops for readability, especially when they are
nested.
for i = 1:m
for j = 1:n
H(i,j) = 1/(i+' ;
end
end
vvhHe
The while loop repeats a group of stateme
6 Programming with MATLAB
%
loop. In nested loops. continue passes control to the next iteration of the for
or while loop enclosing it.
The example below shows a continue loop that counts the lines of
Other Data Structures
You can retrieve our old friend with
M4
Characters and Text
Enter text into MATLAB using single quotes. For example,
5 = 'Hello'
The result is not the same kind of numeric matr
6 Programming with MATLAB
%
Without the preallocation in the previous example, the MATLAB interpreter
enlarges the r vector by one element each time through the loop. Vector
preallocation eliminates t
Scripts and Functions
%
Scripts and Functions
MATLAB is a powerful programming language as well as an interactive
computational environment. Files that contain code in the MATLAB language
are called M
6 Programming with MATLAB
%
switch and case
The switch statement executes groups of statements based on the value of a
variable or expression. The keywords case and otherwise delineate the
groups. Onl
Scripts and Functions
%
MATLAB represents the nonlinear function by a function Mfile. For example,
here is a simplified version of the function humps from the matlabfdemos
directory.
fUnction y = hum
Other Data Structures
2
The statement
sum(M,d)
computes sums by varying the dth subscript. So
sum(M,1)
is a 1by4by24 array containing 24 copies of the row vector
34 34 34 34
and
sum(M,2)
is a 4by
Animations
%
First, decide on the number of frames, say
nframes = 50;
Next, set up the first plot as before, except using the default EraseMode
(normal)
x = rand(n,1)0.5;
y = rand(n,1)U.5;
h = plot(x
Demonstration Programs Included with MATLAB
%
Demonstration Programs Included with MATLAB
MATLAB includes many demonstration programs that highlight various
features and functions. For a complete list
Other Data Structures
%
Other Data Structures
This section introduces you to some other data structures in MATLAB,
including:
 Multidimensional arrays
 Cell arrays
 Characters and text
 Structures
6 Programming with MATLAB
I
Many Mfiles work this way. If no output argument is supplied, the result is
stored in ans. If the second input argument is not supplied, the function
computes a default va
Scripts and Functions
%
However, when using the unquoted form, MATLAB cannot return output
arguments. For example,
legend apples oranges
creates a legend on a plot using the strings apples and oranges
6 Programming with MATLAB
%
generates the 4! = 24 permutations of 1 :4. The kth permutation is the row
vector, p(k, z). Then
A = magic(4);
M = zeros(4,4,24);
for k = 1:24
M(:,:,k) = A(:.p(k.:):
end
st
6 Programming with MATLAB
%
fminsearch(@humps,.5)
T:

0.6370
To evaluate the function at the minimizer,
hLl"IFSOD)
ans =
11.2528
Numerical analysts use the terms quadrature and rhregrarfon to distin
Discrete Mathematics lecture notes 112
November 22, 2013
30. A combinatorial description of the parity of a permutation
In the previous lecture we proved
Theorem 1. For each n , the parity of the num
Discrete Mathematics lecture notes 72
October 17, 2013
23. Trinomial coecients
n
i
Last time we saw that the binomial coecients
formula for powers of sums of two numbers:
n
(a + b)n =
i=0
we had intr
Discrete Mathematics lecture notes 62
October 21, 2013
20. Cantors Theorem
We close our discussion of innite cardinality by showing that, no matter how innitely big a set may
be, there is always one
Discrete Mathematics lecture notes 21
September 14, 2013
4. Truth as a function: Boolean algebra
After proving a handful of theorems, we start to notice that certain logical structures begin to appea
Discrete Mathematics lecture notes 41
September 24, 2013
13. Solving linear equations in Z/n
Last time we began consideration of modular number systems Z/n, and observed that, unlike in Z, a
linear1
6 Programming with MATLAB
%
which produces a numeric row vector containing all of the scores.
ans =
83 91 70
Similarly, typing
S_name
just assigns the names, one at time, to ans. But enclosing the exp
6 Programming with MATLAB
%
r
bar(r)
Typing the statement
magicrank
causes MATLAB to execute the commands, compute the rank of the first 30
magic squares. and plot a bar graph of the result. After exe
5 Graphics
%
Then, instead of creating an entirely new plot, simply change the XData and
Water properties of the original plot.
while 1
drawnow
x = x + s*randn(n,1);
y = y + 5*randn(n,1);
set(h,'XData
Animations
Animations
%
MATLAB provides two ways of generating moving, animated graphics:
 Continually erase and then redraw the objects on the screen, making
incremental changes with each redraw.

Other Data Structures
%
!"#$%&'()*+,.f
0123456789:;s=?
@ABCDEFGHIJKLMNO
PQ RSTWWXYZ Du] 1";
abcdefghijklmno
pqrstuvwxyzcfw_~
'I C'1][email protected] FE
3 see
ii" f m AAOEE
_.un.r.r ?Mcfw_DD
H 1. JanI'll
,"sE