Biomedical Engineering Reference
In-Depth Information
3. Generate a random number and add it to the sum.
Add 1 to the count of random numbers generated.
Go to step 2 to check the sum.
The MATLAB script, shown below, is saved as
whileloop.m
Notice that this sequence has a common structure: each
element of the array is added to the sum. If the vector
was longer, writing out this sequence of statements
would become very tedious; since the statements are
similar, the complete sequence can be replaced with
a shorthand description that uses a for loop:
% whileloop.m
add ¼ 0;
count ¼ 0;
while add < 20
add ¼ add þ rand;
count ¼ count þ 1;
end
display(count);
display(add);
% forloop.m
x ¼ [1 8 3 9 0 1];
add ¼ 0;
for i ¼ 1:length(x)
add ¼ add þ x(i);
end
add
sum (x)
where the for.end loop is the shorthand notation for
the six statements that add each element of x, in turn.
The result of executing forloop.m is:
and is executed from the command window to obtain the
output:
>> whileloop
count ¼
43
add ¼
20.0364
>> forloop
add ¼
22
ans ¼
22
2.1b.3.3.2 For loops
There are variations of iterative control structures, just as
there were variations of the control structures for con-
ditional execution. A for loop (for.end, in MATLAB)
is a repetition structure where the block is repeated
a fixed number of times, once for each value of a control
variable. Typically, the execution of the block changes
with the control variabledfor example, accessing a dif-
ferent element of an array.
Example 2.1b.5 Using for.end loops.
Note that the equivalent of all the statements in the
script forloop.m may be accomplished with a single
command: sum (x). In a small way, this demonstrates
the power of MATLAB.
2.1b.3.4 Encapsulation
The last key to writing well-structured MATLAB pro-
grams that are easy to read, understand and maintain is
encapsulation. Encapsulation means to group together
those instructions or statements that form one particular
function. Encapsulation will improve readability and
make the job of testing and debugging easier. It is hard to
manage one large sequence of MATLAB statements.
Encapsulation is implemented in MATLAB by using
m-files : text files that contain code in the MATLAB lan-
guage. If large MATLAB programs are organized well,
there will be one m-file for the main program and other
m-files for the functions and scripts that are called by the
main program or invoked at the MATLAB command
prompt.
All modern programming languages have features for
encapsulating sequences of instructions. There are typi-
cally two types of encapsulating programming constructs:
subroutines and functions.
A script is a sequence of instructions that does not
accept input arguments or return output arguments. A
script operates on or modifies existing data in the
workspace. In other block structured programming lan-
guages, the organizational equivalent of MATLAB scripts
will be referred to as subroutines.
Given the vector x ¼ [1 8 3 9 0 1], create a MATLAB
script to add the values of the elements (Check your
result with the sum command.)
Solution
The statement of this problem gives the clues that the
script need only add up the values in the given vector, not
an arbitrary vector. You know that the length of the
vector is 6; one way to write the script would be the
commands
x ¼ [1 8 3 9 0 1];
add ¼ 0;
add ¼ add þ x(1);
add ¼ add þ x(2);
add ¼ add þ x(3);
add ¼ add þ x(4);
add ¼ add þ x(5);
add ¼ add þ x(6);
add
sum(x)
Search WWH ::




Custom Search