(although we can’t imagine why you would want to do it this way). If
c
is also a real number the MAT-
LAB command
>> [a:c:b]
or
>> a:c:b
generates a row vector where the difference between successive elements is
c
.
Thus, we can generate num-
bers in any arithmetic progression using the colon operator. For example, typing
>> [18:-3:2]
generates the row vector (18
,
15
,
12
,
9
,
6
,
3). while typing
>> [ pi : -.2*pi : 0 ]
generates the row vector (
π, .
8
π, .
6
π, .
4
π, .
2
π,
0).
Warning:
There is a slight danger if
c
is not an integer. As an oversimplified example, entering
>> x = [.02 : .001 : .98]
′
should
generate the column vector (0
.
02
,
0
.
021
,
0
.
022
, . . .,
0
.
979
,
0
.
98)
T
.
However, because of
round-off errors in storing floating-point numbers, there is a possibility that the last element in
x
will be 0
.
979. The MATLAB package was written specifically to minimize such a possibility,
but it still remains.
†
We will discuss the command
linspace
which avoids this difficulty in sec-
tion 4. An easy “fix” to avoid this possibility is to calculate
x
by
>> x = [20:980]
′
/1000
2.3.
Manipulating Matrices
For specificity in this subsection we will mainly work with the 5
×
6 matrix
E
=
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
,
which can be generated by
>> E = [ 1:6 ; 7:12 ; 13:18 ; 19:24 ; 25:30 ]
Note:
Spaces will frequently be used in MATLAB commands in this subsection for readability.
You can use the colon notation to extract submatrices from
E
.
For example,
>> F = E( [1 3 5] , [2 3 4 5] )
extracts the elements in the first, third, and fifth rows and the second, third, fourth, and fifth columns
of
E
; thus,
F
=
2
3
4
5
14
15
16
17
26
27
28
29
.
†
This possiblity is much more real in the programming language C. For example, the statement
for ( i = 0.02; i <= 0.98; i = i + .001 )
generates successive values of
i
by adding 0
.
001 to the preceding value. It is possible that when
i
should
have
the value 0
.
98, due to round-off errors the value will be slightly larger; the condition
i <= 0.98
will be false
and the loop will not be evaluated when
i
should be 0
.
98.
21

2.3. Manipulating Matrices
You can generate this submatrix more easily by typing
>> F = E( 1:2:5 , 2:5 )
There is an additional shortcut you can use: in a matrix a colon by itself represents an entire row or
column. For example, the second column of
F
is
F(:,2)
and the second row is
F(2,:)
. To replace the
second column of
F
by two times the present second column minus four times the fourth column enter
>> F(:,2) = 2*F(:,2) - 4*F(:,4)
And suppose you now want to double all the elements in the last two columns of
F
.
Simply type
>> F(:,3:4) = 2*F(:,3:4)
Entering
E(:,:)
prints out exactly the same matrix as entering
E
.
This is not a very useful way of
entering
E
, but it shows how the colon operator can work. On the other hand, entering
>> G = E( : , 6:-1:1 )
generates a matrix with the same size as
E
but with the columns reversed, i.e.,
G
=
6
5
4
3
2
1
12
11
10
9
8
7
18
17
16
15
14
13
24
23
22
21
20
19
30
29
28
27
26
25
.

#### You've reached the end of your free preview.

Want to read all 146 pages?

- Fall '13
- Ivan
- matlab, .........