4.1 Vector Indexing

Play media comment.

At 1:47 in the video, I mention logical indexing. 

Let's say we want to extract all elements in the vector that are greater than or equal to 10. We then type: 

x(x >= 10). 

Nice, it works! But why?

Well, if you just type x >= 10 then we get a vector containing logical 0's and 1's. If a specific element is less than 10, then the condition x >= 10 is false, and we get a logical 0, indicating that the statement is false. If a specific element is greater than or equal to 10,  then the condition x >= 10 is true, and we get a logical 1.

So, in MATLAB it would look like: 

x = [2 4 5 8 10 12 3 4 10 13 12 15 13 8]   %Our vector x that we created

>>  x>=10  

[0 0 0 0 1 1 0 0 1 1 1 1 1 0]  %For instance, first element in vector x is 2 which is not greater than or equal to 10, hence we get a logical 0 in the first entry. 

Now, 

x(x >= 10) is equal to the following: x( [0 0 0 0 1 1 0 0 1 1 1 1 1 0] ), which MATLAB interprets as: 

Return all elements where the index is 1. Done!

Logical indexing is used for matrices in the next video.