You can select an element from a vector with parentheses:
>> Y = [6 7 8 9] Y = 6 7 8 9 >> Y(1) ans = 6 >> Y(4) ans = 9
This means that the first element of
6 and the fourth element is
9. The number in parentheses is called the index because it indicates which element of the vector you want.
The index can be a variable name or a mathematical expression:
>> i = 1; >> Y(i) ans = 6 >> Y(i+1) ans = 7
We can use a loop to display the elements of
for i=1:4 Y(i) end
Each time through the loop we use a different value of
i as an index into
In the previous example we had to know the number of elements in
Y. We can make it more general by using the
length function, which returns the number of elements in a vector:
for i=1:length(Y) Y(i) end
This version works for a vector of any length.