Following last tutorial defining vector

How to Accessing elements within a vector

You can view individual entries in this vector. For example to view the first entry just type in the following:

>> v(1)

ans =

2

This command prints out entry 1 in the vector. Also notice that a new variable called ans has been created. Any time you perform an action that does not include an assignment matlab will put the label ans on the result.

To simplify the creation of large vectors, you can define a vector by specifying the first entry, an increment, and the last entry. Matlab will automatically figure out how many entries you need and their values. For example, to create a vector whose entries are 0, 2, 4, 6, and 8, you can type in the following line:

>> 0:2:8

ans =

0 2 4 6 8

Matlab also keeps track of the last result. In the previous example, a variable "ans" is created. To look at the transpose of the previous result, enter the following:

>> ans'

ans =

0

2

4

6

8

To be able to keep track of the vectors you create, you can give them names. For example, a row vector v can be created:

>> v = [0:2:8]

v =

0 2 4 6 8

>> v

v =

0 2 4 6 8

>> v;

>> v'

ans =

0

2

4

6

8

Note that in the previous example, if you end the line with a semi-colon, the result is not displayed. This will come in handy later when you want to use Matlab to work with very large systems of equations.

Matlab will allow you to look at specific parts of the vector. If you want to only look at the first three entries in a vector you can use the same notation you used to create the vector:

>> v(1:3)

ans =

0 2 4

>> v(1:2:4)

ans =

0 4

>> v(1:2:4)'

ans =

0

4

## No comments:

Post a Comment