Introduction to MATLAB and Octave 499
is the window shown in Figure A.5.
FIGURE A.5: The MATLAB help browser
You can find more about the
doc f unction with any of
MATLAB/Octave
>> doc doc
>> help doc
If you want to find help on a particular topic, but do not know the function to use, the
lookfor f unction is extremely helpful. The command
lookfor topic
lists all commands for which the first line of the help text contains the string topic. For
example, suppose we want to find out if MATLAB supports the exponential function
e
x
.
Here’s how:
MATLAB/Octave
>> lookfor exponential
EXP Exponential.
EXPINT Exponential integral function.
EXPM Matrix exponential.
EXPM1 Matrix exponential via Pade approximation.
EXPM2 Matrix exponential via Taylor series.
EXPM3 Matrix exponential via eigenvalues and eigenvectors.
BLKEXP Defines a function that returns the exponential of the input.
and we now know that the function is implemented using exp. We could have used
MATLAB/Octave
>> lookfor exp
and this would have returned many more functions.
500 A Computational Introduction to Digital Image Processing, Second Edition
Note that MATLAB convention is to use uppercase names for functions in help texts,
even though the function itself is called in lowercase.
A.7 Programming
MATLAB and Octave have a very rich programming language. Only a small set of
functions is actually “built in” to each system, other functions are written in the system’s
own programming language. We consider two distinct programs: script files and functions.
Script Files
A script file is simply a list of commands to be executed. It may be that we will execute
the same sequence of commands many times; in which case, it is more efficient to write a
file containing those commands. If the file is called
script.m
and placed somewhere on the
path, then simply entering
script at the prompt will execute all the commands in it. Of
course you can use any name you like for your script file! However, it is convention to end
MATLAB files with the extension
.m.
Functions
As we have seen, a function is a MATLAB command that takes an input (one or several
variables) and returns one or several values. Let’s look at a simple example: writing a
function that returns the number of positive values of a matrix. This function will take a
matrix as input and return a single number as output. We have seen that
MATLAB/Octave
>> a>0
produces a matrix with 1’s in the positions of positive elements. So the sum of all elements
in this new matrix is the number we require. We can obtain the sum of matrix elements
using the
sum function. If applied to a vector, sum produces the sum of all its elements. If
applied to a matrix, however,
sum produces a vector whose elements are the sums of the
matrix columns:
MATLAB/Octave
>> sum(a)
ans =
4 -2 -12 4
>> sum(a>0)
ans =
3 2 0 3
So we have two options here: we can use sum twice:
Introduction to MATLAB and Octave 501
MATLAB/Octave
>> sum(sum(a>0))
ans
=
8
or we could convert the matrix to a vector by using the colon operator before finding the
positive elements:
MATLAB/Octave
>> sum(a(:)>0)
ans =
8
Our function must have a name; let’s call it countpos. A function file starts with the word
function. The first line defines how the function is called. After that come some lines of
help text, and finally the code. Our
countpos f unction is:
MATLAB/Octave
function num = countpos(a)
% COUNTPOS finds the number of positive elements in a matrix. The matrix
can
% be of any data type.
%
% Usage:
%
% n=countpos(a)
num = sum(a(:)>0);
If this file is saved as countpos.m somewhere on the path, we can use countpos exactly as
we use any other MATLAB function or function:
MATLAB/Octave
>> countpos(a)
>> help countpos
>> doc countpos
and the command
MATLAB/Octave
>> lookfor count
will include a reference to countpos.
Finally, if we want to explore functions in more detail, we can use
type, which lists the
entire program corresponding to that function or command. So we can enter
502 A Computational Introduction to Digital Image Processing, Second Edition
MATLAB/Octave
>> type countpos.m
to see a listing of our new function.
Anonymous Functions
An anonymous function is a function that can be generated “on the go”; that is, without
needing to write a complete definition in a file. Such a function can be useful as input to
another function, such as
nlfilter. For example, suppose we want to use a function that
takes a square matrix as input, and produces the scalar product of the means of its rows
with the means of its columns. Such a function can be written as
MATLAB/Octave
>> dotmeans = @(x) sum(mean(x).
*
mean(x’))
This can be applied to any square matrix:
MATLAB/Octave
>> A = randi([1,10],6,6)
A =
8 2 6 1 9 8
6 1 5 6 1 3
6 7 9 2 9 10
7 2 8 1 4 8
7 4 10 1 7 5
1 4 10 1 5 1
>> dotmeans(A)
ans = 167.06
Note here that randi is a handy function that produces a random matrix of integers with
low and high values given in the first parameter, and the size of the last two values.
Exercises
1. Perform the following calculations:
132 + 45, 235 × 645, 12.45/17.56. sin(π/6), e
0.5
,
2
2. Now enter
format long and repeat the above calculations.
3. Read the help file for
format, and experiment with some of the other settings.
4. Enter the following variables: a = 123456, b = 3
1/4
, c = cos(π/8). Now calculate:
(a + b)/c, 2a 3b, c
2
a b, a/(3b + 4c), exp(a
1/4
b
10
)
Introduction to MATLAB and Octave 503
5. Find the functions for the inverse trigonometric functions arcsin, arccos, and arctan.
Then calculate :
arcsin(0.5), arccos(
3/2), arctan(2)
Convert your answers from radians to degrees.
6. Using vectorization and the colon operator, use a single command each to generate:
(a) The first 15 cubes
(b) The values sin(/16) for n from 1 to 16
(c) The values
n f or n from 10 to 20
7. Enter the following matrices:
A =
1 2 3
2 3 4
3 4 5
, B =
1 2 1
3 4 5
2 3 4
, C =
0 2 1
3 5 2
1 1 7
Now calculate:
2A 3B, A
T
, AB BA, BC
1
, (AB)
T
, B
T
A
T
, A
2
+ B
3
8. Use the det function to determine the determinant of each of the matrices in the
previous question. What happens if you try to find the inverse of matrix A?
9. Write a little function issquare, which will determine whether a given integer is a
square number. Thus:
MATLAB/Octave
>> issquare(9)
ans =
1
>> issquare(9.000)
ans =
1
>> issquare(9.001)
ans =
0
>> issquare([1:10])
ans =
1 0 0 1 0 0 0 0 1 0
..................Content has been hidden....................

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