Page 20 - Basics of MATLAB and Beyond
P. 20
>> a = zeros(2,3)
a =
0 0 0
0 0 0
>> b = ones(2,2)/2
b =
0.5000 0.5000
0.5000 0.5000
>> u = rand(1,5)
u =
0.9218 0.7382 0.1763 0.4057 0.9355
>> n = randn(5,5)
n =
-0.4326 1.1909 -0.1867 0.1139 0.2944
-1.6656 1.1892 0.7258 1.0668 -1.3362
0.1253 -0.0376 -0.5883 0.0593 0.7143
0.2877 0.3273 2.1832 -0.0956 1.6236
-1.1465 0.1746 -0.1364 -0.8323 -0.6918
>> eye(3)
ans =
1 0 0
0 1 0
0 0 1
3.4 Subscripting
Individual elements in a matrix are denoted by a row index and a column
index. To pick out the third element of the vector u type:
>> u(3)
ans =
0.1763
You can use the vector [123] as an index to u. To pick the first three
elements of u type
>> u([1 2 3])
ans =
0.9218 0.7382 0.1763
Remembering what the colon operator does, you can abbreviate this to
c 2000 by CRC Press LLC