494 A Computational Introduction to Digital Image Processing, Second Edition
MATLAB/Octave
>> [1:10].^2
ans
=
1 4 9 16 25 36 49 64 81 100
What happens here is that
[1:10]
generates a vector consisting of the numbers 1 to 10;
and the dot operator
.^2
squares each element in turn.
Vectorization can also be used with logical operators; we can obtain all positive elements
of the matrix
a
above with:
MATLAB/Octave
>> a>0
ans =
1 0 0 1
1 1 0 1
1 0 0 0
0 1 0 1
The result consists of 1’s only in the places where the elements are positive.
MATLAB and Octave are designed to perform vectorized commands very quickly, and
whenever possible such a command should be used instead of a
for loop.
Cell Arrays
Cell arrays are able to hold different objects: matrices of different sizes, images, or any
other data. They are delineated with braces, and can be defined by listing all values:
Introduction to MATLAB and Octave 495
MATLAB/Octave
>> C = {[37],ones(2),randi([10,20],3,3),magic(4)}
C =
{
[1,1] = 37
[1,2] =
1 1
1 1
[1,3] =
15 14 13
19 18 12
15 17 16
[1,4] =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
}
Elements of
C
can be obtained again by using braces:
MATLAB/Octave
>> C{3}
ans =
15 14 13
19 18 12
15 17 16
A cell array can be extended simply by putting a value into an index; any intermediate
values will be filled with empty arrays:
MATLAB/Octave
>> C{10} = "Now is the winter of our discontent";
>> C{5:10}
ans = [](0x0)
ans = [](0x0)
ans = [](0x0)
ans = [](0x0)
ans = [](0x0)
ans = Now is the winter of our discontent
For another example, suppose we cre ate a cell array each of whose elements is a list of the
binomial coefficients:
496 A Computational Introduction to Digital Image Processing, Second Edition
MATLAB/Octave
>> B = {[1]}
>> for i = 2:11, B{i} = [B{i-1} 0]+[0,B{i-1}]; end
>> B{7}
ans =
1 6 15 20 15 6 1
The entire cell array can be displayed with celldisp.
The
cellfun f unction maps a function onto each value of a cell array:
MATLAB/Octave
>> cellfun(@(x) sum(x), B)
ans =
1 2 4 8 16 32 64 128 256 512
1024
There are several other functions for managing cell arrays; lookfor cell will display a
list which includes them.
A.5 Plots
MATLAB and Octave have outstanding graphics capabilities. But we shall just look at
some simple plots. The idea is straightforward: we create two vectors
x and y of the same
size, then the command
>> plot(x,y)
will plot y against x. If y has been created from x by using a vectorized function f(x), the
plot will show the graph of y = f(x) . Here is a simple example:
MATLAB/Octave
>> x = [0:0.1:2
*
pi]is;
>> plot(x,sin(x))
and the result is shown in Figure A.3. The plot f unction can be used to produce many
different plots. We can, for example, plot two functions simultaneously with different colors
or plot symbols. For example:
MATLAB/Octave
>> plot(x,sin(x),’.’,x,cos(x),’--r’,’linewidth’,2)
produces the graph shown in Figure A.4.
Introduction to MATLAB and Octave 497
FIGURE A.3: A simple plot in MATLAB/Octave
FIGURE A.4: A different plot in MATLAB/Octave
498 A Computational Introduction to Digital Image Processing, Second Edition
A.6 Online Help
A vast amount of online help and information is available. So much, in fact, that it
is quite easy to use MATLAB or Octave without a manual. To obtain information on a
particular command, you can use
help
. For example:
MATLAB/Octave
>> help for
FOR Repeat statements a specific number of times.
The general form of a FOR statement is:
FOR variable = expr, statement, ..., statement END
The columns of the expression are stored one at a time in
the variable and then the following statements, up to the
END, are executed. The expression is often of the form X:Y,
in which case its columns are simply scalars. Some examples
(assume N has already been assigned a value).
FOR I = 1:N,
FOR J = 1:N,
A(I,J) = 1/(I+J-1);
END
END
FOR S = 1.0: -0.1: 0.0, END steps S with increments of -0.1
FOR E = EYE(N), ... END sets E to the unit N-vectors.
Long loops are more memory efficient when the colon expression appears
in the FOR statement since the index vector is never created.
The BREAK statement can be used to terminate the loop prematurely.
See also IF, WHILE, SWITCH, BREAK, END.
If there is too much information, it may scroll past you too fast to see. In such cases, you
can turn on the MATLAB pager with the command
MATLAB/Octave
>> more on
For more help on help, try:
MATLAB/Octave
>> help help
Better formatted help can be obtained with the doc function, that opens up a help browser
which interprets HTML-formatted help files. The result of the command
MATLAB/Octave
>> doc help
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.80.173