Accessing/Indexing Vectors
For these notes, please assume the following definitions
:
vec = [45 11 37];
vec2 = [32 15 47];
1.
What is indexing?
a.
Indexing a vector is known as either changing the values in a vector or getting the
values in a vector by referencing the position.
b.
x = vec(1)
b.i.
The line of code above goes to the 1 position in vec, and gets the value. In
this case, it's 45.
c.
x = vec(45)
c.i.
The line of code above
does not
go to the value 45. It goes to the 45th
position in the vector. Since there is no 45th position, matlab reads and
error.
2.
Assigning/Getting values of single positions.
a.
It's possible to change/get the single values in a vector.
b.
Accessing Values
b.i.
x = vec(1)
b.i.1.
The code above gets the value in the 1st position. In this
case, x would become 1.
b.ii.
x = vec(
i
)
b.ii.1.
The above is a general form. X will be set to the
ith
value
in the vector.
c.
Assigning values
c.i.
vec(1) = 60
c.i.1.
The code above assigns 60 to the first value of the vector.
Therefore, vec> [60 11 37].
c.ii.
vec(6) = 60
c.ii.1.
The code above attempts to replace the value "45" in vec,
and replace it with 60.
But
, remember that the value between the
parenthesis refers to the
position
and not the value.
c.ii.2.
Matlab will go ahead and assign the 6th position in vec to
60.
c.ii.2.a.
But there's no 6th position? So what happens?
c.ii.2.b.
Matlab does not error. Instead, matlab will extend
the vector to assign 60 to the 6th value.
c.ii.2.c.
With the code above, vec > [ 45 11 37 _ _ 60]
c.ii.2.d.
You will notice that there are empty positions. What
does matlab do? Well, it fills them in with zeros.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '08
 Stallworth
 Computer Science, matlab, Prime number, Vector Motors, vec, d.i. vec

Click to edit the document details