Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1
Hello everyone and welcome to the lecture on vector indexing and slicing and this lecture on learn how
2
to use bracket notation to index and access individual elements from a vector.
3
Let's go ahead and jump to our studio.
4
All right.
5
So here we are our studio and to start things off.
6
I'm just going to go ahead and make vectors the one and the two will say V-1.
7
It's just a numeric vector consisting of 100 200 and 300 and we'll say vector V-2 is a vector with a
8
b and c is a character vector.
9
So A B C.
10
All right.
11
So we have the one we have the to know what we're going to do is learn how to use bracket notation to
12
index.
13
So indexing works by using brackets and then passing the index position of the element as a number.
14
And keep in mind that indexing and are starts at 1 and some other programming languages such as Python
15
indexing starts at zero.
16
That in our IT STARTS AT 1.
17
So let me go ahead and show you an example of how to grab elements from a vector.
18
You'll put in the vector and then you'll use square brackets and then you'll pass in the index of the
19
element you want.
20
So for example let's say you want the elements 200 in the vector of the one.
21
So it's at position to this in the second position.
22
So I input the index two inside of my square brackets and it returns to me the elements at that index
23
.
24
So let's do another example let's say I wanted the letter C.
25
So that's an vector v 2 and in square brackets I'm going to input 3 because it's at the third position
26
.
27
OK.
28
And then it returns that particular elements as far as slicing.
29
That means we're going to do basically multiple indexing so we can grab multiple items from a vector
30
by actually passing a vector of index positions inside the square brackets.
31
Let's go ahead and show an example of that first thing to do is just clear the consul showed the one
32
and showed it to.
33
All right.
34
So let's go ahead and grab the numbers 100 and 200.
35
So those are from Vector 1.
36
And if I just put in a single number I'll return a single element but to grab an actual slice of that
37
vector I can pass in a vector inside of the square brackets and this vector will just be a vector of
38
index positions.
39
So I want to grab the first element and the second element and that's what occurs.
40
So let's say I wanted to grab the letters A and see.
41
So that's in Dr. V-2.
42
I put in my square brackets to notate that I'm using indexing and then I want to grab and see.
43
So that's the first element and the third elements.
44
So I'm going to go ahead and put in a vector.
45
Notice I'm using the combined function to specify that it's a vector and I'm going to go ahead and grab
46
a and c in that manner.
47
So that's how you can grab multiple items from a vector.
48
So you heard me use the term slicing earlier and what I mean by slicing is not just any multiple indexing
49
It's when you can actually grab a continuous slice of vector.
50
So let's go ahead and show you how you can do that in order to do that.
51
I'm going to go ahead and create a longer vector.
52
We'll call this just the.
53
And we'll do it on the numbers one through ten.
54
One two three four five six seven eight nine.
55
And then finally 10 there are actually easier easier and better ways to create such a vector which will
56
show you later on in the course.
57
But for now we'll just use than normal combine function.
58
So my vector which is the numbers 1 through 10.
59
And let's actually want to grab a slice so that continuous slice of elements I can use that using a
60
colon notation.
61
So I'm going to use square brackets again and then the weight of notation or syntax works as you put
62
your starting index.
63
So let's say I want to grab the numbers 2 3 and 4.
64
So I want to start a position to then I'm going to put in the colon and then put my stopping index.
65
So I want to stop at four.
66
So this will return the numbers 2 3 and 4.
67
So that's going to grab that slice.
68
Let's go ahead and show another example.
69
Let's say I wanted to grab the elements 7 8 9 and 10.
70
So output outputted my vector put in square brackets and I want to start at index position 7 and ends
71
or stop in the x position 10.
72
So that's going to be my slice and I get in return 7 8 9 and 10.
73
Now previously in the course we discussed using names to name the elements in our vector such as that
74
temperature vector we saw earlier.
75
We can actually use those names as index marks.
76
So let's go ahead and show how you can index elements using names in order to do this.
77
I'm going to go ahead and reassign V as just a simple vector of numerics we'll call it one two three
78
four and then I'm going to use the names function on the.
79
And I'm going to go ahead and pass on a vector of just names.
80
In this case it'll just be simple letters to designate the names for each of those elements.
81
So say B C D and make sure to put some quotes around that.
82
And there we go.
83
So now if I look at V it has those named elements 1 2 3 4.
84
So instead of having to do something like the two which will return that names element B to what I can
85
do is actually pass in the name of the elements I want so element 2 is going to be named B.
86
So instead of passing in an index it can actually pass in the name B and I'll get that also back in
87
return the same way.
88
And if you want to do multiple indexing you can actually pass in a vector.
89
So I can do something like this I can save the square brackets combine function and then pass in of
90
vectors of names.
91
So I can do this actually out of order.
92
Let's go ahead and say I want names see the element name Z and the element names a and r is going to
93
return those named elements in the same order as I requested them in that vector.
94
OK so now let's do a brief discussion on comparison operators and selection.
95
So as we discussed in the comparison operator lecture we can use comparison operators to filter out
96
elements from a vector.
97
Sometimes this is also referred to as boolean or logical masking because you're essentially creating
98
a vector of logicals to filter out results you want.
99
Let's go ahead and see an example.
100
We'll use the same vector we've been working with the ABC 1 2 3 4.
101
So let's say I wanted to filter out any values that were less than 2.
102
So I wanted to only grab values that were greater than two in this case going to be three and four.
103
I can do that same bracket notation.
104
In this case I just do a comparison operator inside of that bracket notation.
105
So it kind of looks like this.
106
Now I have the square brackets and then a comparison condition here.
107
So basically says Return V were V is greater than 2.
108
So if I click enter I see that C and D name which are both greater than two three and four then returns
109
.
110
So to break down and see how this actually works let's first show what is returned if we just say V
111
is greater than 2.
112
So if I say V is greater than 2.
113
Notice I get a logical vector out I get false false.
114
True true.
115
And so what this means is that we're basically passing this vector of logicals through the brackets
116
of the vector and only return TRUE values at the matching index positions.
117
What's also nice about this is we can assign that condition that logical comparison to a variable name
118
and then just pass in that variable name and the brackets.
119
So for example I could say something like this.
120
I could say filter Let's actually call this something assholes call it my filter.
121
So you don't overwrite any builtin functionality will say my filter will go ahead and put in the logical
122
comparison I will say the is greater than 2.
123
So now it's my filter.
124
And if you notice then my filter is just this vector.
125
So I can then say when it clear that the pass in my filter and it'll return where those explanations
126
are true.
127
So in this case it's C and D.
128
So this is really nice because you can start to name your comparison operators are these logical conditioned
129
statements pass them into vectors that have names.
130
So everything becomes a lot more readable later on in your code.
131
All right.
132
So that's it for this lecture.
133
Just a brief overview of what we've covered.
134
We learned how we can use bracket notation to index and access individual elements from a vector.
135
We learned how we can use that same notation for multiple indexing by passing a vector inside of those
136
brackets.
137
And we also learn how to use slicing with that colon in order to grab a continuous slice of the vector
138
and then finally we learn how we can index of names and also use comparison operators in logical conditions
139
statements to filter out results from a vector.
140
All right thanks everyone and I'll see you at the next lecture
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.