Another very important type of indexing is logical indexing.
In the prior videos, we discussed linear and array
indexing. We would just give the number of the element that we want to access. But here, we can use a Boolean array to access the elements conditional on a logical statement. For example, if we want to take the elements less than some number, we can extract them from the array. The way that this works is it matches each element in a Boolean array to those in the original array with a one-to-one correspondence only if the Boolean expression at that element, i, is true. This is also very efficient. The way that this would be written, if we wrote out all the for loops in a language such as C++, is we would be looping through the elements sequentially, checking if they met this logical criteria, and if so, extracting them. So in this sense we have the nice linear and component-wise available. For this reason, in the below example, we can see that this is much quicker than using find and then vectorized indexing. What find returns, if we look at the find of P less than 0.5, is it's going to extract all the indices where P is less than 0.5. Then when you access P at those indices, you're not going in linear memory order. You're going to be going out of order, which can be slow on large arrays, which is why you always want to use logical indexing in the first example here, rather than find. You'll also note that in MATLAB, usually when you're using find, it will underline it in orange, giving you a warning to replace it with logical indexing. So it is very important to pay attention to the warnings in MATLAB, because they can greatly help increase the speed of your code. So here in this example, what we see we're doing is we're forming P, which is a random 5,000-by-5,000 matrix, so a large problem. And we're only going to print the value such that P is less than 0.5. So for this, using tic and toc, we'll time it only takes 6 seconds. If we first find the indices and then access them in P, it takes 9 seconds. And for larger arrays, this difference would be even more drastic. So another example here, just showing that logical indexing and find get the same expression. Here we form a 5-by-5 random matrix, and we extract four entries that are less than 0.5. isequal verifies to us that this is the exact same as if we used the find function. Here, let's take a break for our first exercise, which is using logical indexing. Here, we have x being a linearly spaced vector from 0 to 2 pi, and in the code given in logarray_assign.m, we are plotting the sine of 2x at those values. What I would like you to do would be here, to plot only the values in the interval from 0 to pi/2, using one line of code. So what you need to do is just fill in the blanks for what would be x and what would be y, using logical indexing. In the second example, what we have here is I'd like you to bound the y value. So now we only want to plot such that negative 0.5 is less than y, which is less than 0.5. And we'll see that in MATLAB, this can be done very simply using one line of code. I hope you've had time to pause the video and to try the exercise. And now we will go over the solution together. First of all, let's run what log_assign does, and we see that we have the sine curve plotted for all of 2pi. So now if we want to plot it using the x constraint, we can use logical indexing. So we want x to be less than 2pi. We have this logical array, which we access the x elements, and we can also use the same to extract the corresponding y components. And here just means we're plotting this dashed in red. Now for the second constraint, we were bounding the values of y So we want y to be less than 0.5 and greater than negative 0.5. This is the logical array that we pass into both x and to y for the plot. Very, simple and can be done in two lines of code. Just as plotting notes linewidth 2, we're increasing the plot, so we can see it a little bit better. Now if we run this line of code, we'll get the following plot. We see the red curve is where x is less than zero less than pi/2, and the blue dashed curve is capping y at 0.5. So this is just a nice little exercise to show you the advantage of logical indexing and how you can write nice, clean code using it.