5.7) Array Indexing
- We may want to access elements of the vector/matrix
- For example, suppose we want to access a particular element of vector "a". First remember that "a" has dimensions 1x10
- We can access the elements of vector "a" by knowing that each element has an index
- As "a" has 10 elements, the indices identifying these elements run from 1 to 10. Therefore we can access the 5th element by using the following syntax:
- We can use the colon operator to specify indices that we are interested in. For example, to extract the last 5 elements of vector "a", we can use:
[matlab]
>> a=[1 2 3 4 5 6 7 8 9 10];
[/matlab]
[matlab]
>> a(1,5)
ans=5
[/matlab]
[matlab]
>> a(1,5:10)
ans= 5 6 7 8 9 10
[/matlab]
This will output the vector "ans" with size 1x5 containing the last 6 elements of vector "a".