Sie sind auf Seite 1von 5
Lab 2 ‐ Fundamentals of MATLAB Headlines • M ‐ Files • Operators • Flow

Lab 2 Fundamentals of MATLAB

Headlines

MFiles

Operators

Flow Control Structures

Histogram

MFiles

The MATLAB® software provides a full programming language that enables you to write a series of MATLAB statements into a file and then execute them with a single command. You write your program in an ordinary text file, giving the file a name of filename.m .

Creating MFiles

Mfiles are ordinary text files that you create using a text editor. If you use the MATLAB Editor/Debugger, open a new file by selecting New > M File from the File menu at the top of the MATLAB Command Window.

Types of MFiles

Scripts:

• Are useful for automating a series of steps you need to perform many times.

• Do not accept input arguments or return output arguments.

• Store variables in a workspace that is shared with other scripts and with the MATLAB command line interface.

Functions:

• Are useful for extending the MATLAB language for your application.

Can accept input arguments and return output arguments.

• Store variables in a workspace internal to the function.

function [out1 out2] = fun_name(in1,in2) out1 = in1 + in2; out2 = (in1 + in2)/2;
function [out1 out2] = fun_name(in1,in2)
out1 = in1 + in2;
out2 = (in1 + in2)/2;
end

Operators

Arithmetic Operators

MATLAB has two different types of arithmetic operations. Matrix arithmetic operations are

defined by the rules of linear algebra. Array arithmetic operations are carried out element by element. The dot (.) character distinguishes array operations from matrix operations.

distinguishes array operations from matrix operations. Exercise: Write a function take a image name as input

Exercise:

Write a function take a image name as input parameter and display the negative of this image .

Relational and Logical Operators

These operators compare (or perform logical operation on) corresponding elements of arrays of equal dimensions element byelement, and produce a logical array.

Operator

Name

&

AND

|

OR

~

NOT

<

Less than

<=

Less than or equal

>

Greater than

>=

Greater than or equal

==

Equal to

~=

Not equal to

Exercise:

Matrix A=

Return A elements which are >4

Some Important Variables and Constants

357

238

>4 Some Important Variables and Constants 357 238 Flow Control MATLAB provides the conventional flow

Flow Control

MATLAB provides the conventional flow control statements needed in structured programming. Keep in mind that MATLAB treats a logical 1 or any nonzero number as true and 0 as false .

1 or any nonzero number as true and 0 as false . if, else, and elseif

if, else, and elseif

if logical_expression1 statements1 elseif logical_expression2 statements2 else statements3 end
if logical_expression1
statements1
elseif logical_expression2
statements2
else
statements3
end

for

for index = start:increment:end statements end
for index = start:increment:end
statements
end

while

while expression statements end
while expression
statements
end

switch, case, and otherwise

switch expression %(scalar or string) case value1 statements1 % Executes if expression is value1 case
switch expression %(scalar or string)
case value1
statements1 % Executes if expression is value1
case {value2, value3,…}
statements2 % Executes if expression is value2
otherwise
statements3 % Executes if expression does not
% match any case
end

Vectorizing Loops

The MATLAB® software uses a matrix language, which means it is designed for vector and matrix operations. You can often speed up your Mfile code by using vectorizing algorithms that take advantage of this design. Vectorization means converting for and while loops to equivalent vector or matrix operations. Simple Example of Vectorizing Here is one way to compute the sine of 10001 values ranging from 0 to 10:

tic %Start timing i = 0; for t = 0:0.01:100 i = i + 1;
tic %Start timing
i = 0;
for t = 0:0.01:100
i
= i + 1;
y(i) = sin(t);
end
t1=toc; %End timing

A vectorized version of the same code is tic %Start timing

t = 0:0.01:100; y = sin(t); t2=toc; %End timing %Compute the ratio of the two
t
= 0:0.01:100;
y
= sin(t);
t2=toc; %End timing
%Compute the ratio of the two times
rt = t1/(t2+eps); %Use eps in case t2 is close to 0

The second example executes much faster than the first and is the way MATLAB is meant to be used. Test this on your system by creating Mfile scripts that contain the code shown, and then using the tic and toc functions to time the Mfiles.

Histogram processing

The histogram of a digital image with possible intensity levels in the range [0 G] is defined as a discrete function with points:

Where: is the intensity in range 0 ; is the number of pixels with this intensity.

imhist(f, b); h = imhist(f, b); %Store histogram in h
imhist(f, b);
h = imhist(f, b); %Store histogram in h

Where: f

is input image,

b

is the number of bins in the output (default 256)

Exercise:

How many pixels have intensity in the range [0,32] in ‘cameraman.tif’?