Digital Signal Processing Reference
In-Depth Information
of precision if and when it is necessary, for example, when we use the functions
and scripts in the Signal Processing Toolbox.
9.1.7 Control Flow
Three functions are used to control the flow of command execution that depend
on decisionmaking statements. Such functions are found in other programming
languages and also in MATLAB. They are for loops , If-elseif-end loops
and while loops , which will be illustrated below.
The statement
>> for n=1:10
x(n )=n^2+4*n
end
n 2
produces a vector containing 10 values of x(n)
1 , 2 , 3 ,..., 10.
Onecandefineanarraysuchas n=3:-0.5:-1.0 , in which the increment
is 0.5 and the result is an array n = [3.0 2.5
=
+
4 n ,for n
=
2.0
1.5
1.0 0.5
0.0
-0.5 -1.0] . The default value for the increment is 1.
To define a two-dimensional array, and a function H(i,j)=0.1^i+0.2^j ,we
use the statements
for i =1:20;
for j =1:20;
X(i,j) = 0.1^i+0.2^j
end
end
An example of the use of the if statement is
>>n=-10:10
if n<0
x(n)=0;
elseif 0 n 5;
x(n)=(0.8).^n;
else
x(n)=0
end
Note that an error message will be shown if we use the statement
x(n) = (0.8)^n without the dot before the exponent.
The while loop is executed step by step as long as the relation holds true.
An example of this is
>>n=1
while n<8
x(n)=0.5^n;
n=n+1
end
Search WWH ::




Custom Search