MATLAB has control statements like those in most computer languages. We will only study the for loop here. See the MATLAB manual
for details on if and while statements.
What for loops do is allow a statement or a group of statements to be repeated. For example,
for i = 1:n,x(i) = 0,endassigns the value 0 to the first n elements of the array x. If n is less than 1,
the instruction is still valid but nothing will be done. If n is not defined, then
the following message will appear:
??? Undefined function or variable.
Symbol in question==>n
If n contains a real value, the integer part of n is used. If n is a complex
number, the integer part of the real part is taken. (This should, however, be
avoided.) If x is not declared earlier or is of smaller dimension than n, then
extra memory is allocated.
The command end must follow the command for. If it is not present, MATLAB will wait for remaining statements until you type the command end, and nothing will be executed in the meantime.
More than one statement may be included in the loop. The statement 1:n is the way to specify all the integer values between 1 and n. A
step different than 1 may be specified. For example, the statement for i=1:2:5,x(i)=1,end is equivalent to x(1)=1,x(5)=1. Negative steps
are also allowed, as in i=n:-1:1.
We may use a for loop to draw a circle of radius 1. Type
≫ j=sqrt(-1);
≫ n=360;
≫ for i=1:n,circle(1)=exp(2*j*i*pi/n);end;
≫plot(circle)Note how easy it is to plot a curve. But also note how slowly the for loop
is executed. MATLAB is not very good at executing things one by one. It
prefers, by far, a vector-oriented statement. Using the range specification as
in the for loop, it is possible to speed up the process by replacing the explicit
for loop by an implicit for loop using the colon, like this:
≫ circle = exp((2*j*pi/n)*[1:n]);Note how much faster this command is executed. In general, for loops should
be avoided as much as possible. For example, the first for loop you typed
could have been replaced by the command x=zeros(1,n);, which is much
more efficient. The function zeros fills the variable with 0's to the specified
size. Similarly, the function ones fills the variable with 1's. The size can also
be determined by the size of the argument. If A is a matrix of size m,n, then
the command B=ones(a) fills the matrix B with 1's and forces the matrix B
to have exactly the same size as the matrix A.
Avoiding for Loops. Since for loops are very inefficient in MATLAB
(they are computed sequentially, adding several more computations for every
loop), it is preferable to use the matrix capabilities of MATLAB to replace
for loops and speed up processing time.
≫ for i = 1:10,
≫ x(i) = i;
≫ end;≫ x = 1:10x = [1 2 3 4 5 6 7 8 9 10]for loop≫ z = something;
≫ for i = 1:10,
≫ x(i) = z*i;
≫ end;≫ z = something
≫ x = z*(1:10);x = [z 2*z 3*z ... 10*z]for loop≫ z = something;
≫ x(1) = z;
≫ for i = 2:10.
≫ x(i) = z*x(i-1);
≫ end;≫ z = something;
≫ x = z.^(1:10);x = [z z**2 z**3 ... z**10]for loop≫ for i = 0:2:100,
≫ x(i) = 1.0*exp(j*2*pi*i/100);
≫ end;≫ x = 1.0*exp(j*2*pi*(0:2:100)/100);x = [exp(0) exp(j*4*pi/100) ... exp(j*200*pi/100)]
"Reviewer's Comments: 'I recommend this book as a "required primary textbook." This text attempts to lower the barriers for students that take courses such as Principles of Electrical Engineering, […]"