Lecture 6 Introduction To M Function Programming
Lecture 6 Introduction To M Function Programming
Qadri Hamarsheh
function [s p] = sumprod (f , g)
Keyword The
product Input
image images
Brackets:
Must be (multiple output).
May be (one output).
Without brackets or equal sign (no outputs).
• Function calling:
>> x= imread ('someimage1.tif');
>> y= imread ('someimage2.tif ');
Dr. Qadri Hamarsheh
>> [s , p] = sumprod (f , g);
b) H1 line:
• Single comment line that follows the definition line (no blank lines) between them.
% SUMPROD computes the sum and product of two input images.
• H1 line appears when user types
>> help function_name
• H1 provides information about the M-file.
c) Help text:
• Text block follows H1 (without any blank lines in between the two).
• Help text is used to provide comments and online help for the function, when user
type:
>> help function_name:
• Matlab displays all comments lines that appear between the function name and first
(executable or blank) line.
d) Function body:
• Matlab code that performs computation.
e) Comments.
• %- symbol used for comments declaration.
Operators:
Arithmetic: numeric computations
Relational: compare operands quantitively.
Logic: perform AND,
AND OR and NOT.
NOT
Arithmetic operators:
Matrix arithmetic.
Array arithmetic.
Example 6.1
function [p, pmax, pmin, pn] = improd (f , g)
% IMPROD computes the product and other parameters of two images.
% [ P, PMAX, PMIN, PN] = IMPROD (F , G) output the
% element-by-element product of two input images,
% F and G, the product maximum and minimum
% values, and a normalized product array with values
% in the range [0 , 1]. The input images must be
% of the same size. They can be of class uint8,
% uint16, or double. The outputs are of class double.
fd = double (f);
gd = double (g);
p = fd.*gd;
pmax = max (p (:));
pmin = min (p (:));
pn = mat2gray (p);
• Note: the input images were converted to double using the function double instead of
im2double, because if the inputs were of type uint8, im2double would convert them
to the range [0, 1]. (We want p to contain the products of the original values).
Using the function (calling)
>> f = [1 2 ; 3 4];
>> g = [1 2 ; 2 1];
>> [p , pmax, pmin, pn] = improd (f , g)
p =
pmax = 6
pmin = 1
.
pn =
. .
>> help improd
Relational operators
RELATIONAL OPERATOR
OPERATOR NAME
< Less than
<= Less than or equal
> Greater than
>= Greater than or equal
== Equal to
~= Not Equal to
Example1:
Consider the following sequence of inputs and outputs: Both A and B must
>> A = [1 2 3; 4 5 6; 7 8 9]; be the same size
>> B = [0 2 4; 3 5 6; 3 4 9];
Dr. Qadri Hamarsheh
>>A==B
ans =
0 1 0
0 1 1
0 0 1
• Operation A==B produces a logical array of the same dimension as A and B with 1s in
locations where the corresponding elements of A and B match, and 0s elsewhere.
• Example2:
>> A >= B
ans
1 1 0
1 1 1
1 1 1
• A >= B produces a logical array with 1s where the elements of A greater than or
equal to the corresponding elements of B and 0s elsewhere.
• If one operand is a scalar ⇒matlab tests the scalar against every element of the other
operand.
Logical operators