Page 21 - Basics of MATLAB and Beyond
P. 21
>> u(1:3)
ans =
0.9218 0.7382 0.1763
You can also use a variable as a subscript:
>> i = 1:3;
>> u(i)
ans =
0.9218 0.7382 0.1763
Two dimensional matrices are indexed the same way, only you have
to provide two indices:
>>a=[123;4 56;789]
a =
1 2 3
4 5 6
7 8 9
>> a(3,2)
ans =
8
>> a(2:3,3)
ans =
6
9
>> a(2,:)
ans =
4 5 6
>> a(:,3)
ans =
3
6
9
The last two examples use the colon symbol as an index, which matlab
interprets as the entire row or column.
If a matrix is addressed using a single index, matlab counts the
index down successive columns:
>> a(4)
ans =
2
>> a(8)
ans =
6
Exercise 1 Do you understand the following result? (Answer on
page 183.)
c 2000 by CRC Press LLC