Sunday, 31 July 2016

MATLAB Programming 41 - Some vector operations (2)

Referencing the Elements of a Vector


You can reference one or more of the elements of a vector in several ways. The ith component of a vector v is referred as v(i).

For example:


v = [ 1; 2; 3; 4; 5; 6]; % creating a column vector of 6 elements

v(3)

MATLAB will execute the above statement and return the following result:


ans =
3

When you reference a vector with a colon, such as v(:), all the components of the vector are listed.


v = [ 1; 2; 3; 4; 5; 6]; % creating a column vector of 6 elements

v(:)


MATLAB will execute the above statement and return the following result:


ans =
1
2
3
4
5
6

MATLAB allows you to select a range of elements from a vector.

For example, let us create a row vector rv of 9 elements, then we will reference the elements 3 to 7 by writing rv(3:7) and create a new vector named sub_rv.


rv = [1 2 3 4 5 6 7 8 9];
sub_rv = rv(3:7)

MATLAB will execute the above statement and return the following result:


sub_rv =

3 4 5 6 7



No comments:

Post a Comment