How to Apply Logical Indexing and Then a Subscript to a Vector in One Line of Code

Let's say I have a vector
a=(linspace(1,10,10))';
And a set of logicals to shrink this vector
b=logical(zeros(10,1));
b([1:2, 5:6])=logical(1);
c= a(b);
And d is a subcript(s) that comes from some other vector (e) that is the same size as c
e=rand(size(a(b),1),1);
[~,d]=max(e);
Now I want to create vector f
f=c(d);
However, I would like to avoid this middle step of creating and calling for c due to the size of my vectors. The theoretical b and d vectors are readily available from previous steps. In my head, this looked like the following
f=(a(b))(d);
Unfortunately, that did not work. Any help is much appreciated.

 Accepted Answer

MATLAB doesn't support cascaded indexing. You could either split the computation into two lines like you've done (and which I recommend!); or write a function that takes in a(b) and d and does the indexing. Either way, MATLAB has to create the intermediate vector so you're not gaining anything but you would be masking the computation with the latter.
f = foo(a(b), d)

4 Comments

Is there some particular reason that you recommend using two lines rather than writing a function here or is it just because I didn't make it clear how often I may have to do this? In this particular instance, I will need to do this 20-30 times, so a function certainly sounds like a better option at face value.
The same operation 20-30x? Can you give more detail, there might be a way around it.
The function just obfuscates it. As someone reading your code, I can clearly see what's happening here:
Agood = a(b)
d = Agood(idx)
I have no clue here:
d = getGoodValue(a(b),idx)
I could open the function but that's more effort than an extra line of code. Code lines don't cost money so use them liberally!
I'll do my best to not be too vague here and will say that it is highly unlikely that anyone else ever ends up reading this code. While I will agree that two lines are easier to read than the one line, a quick comment is easy enough to throw into a section of code especially for such a simple function.
I am working on a parameter study in which I am concerned with up to 16 parameters in the case of this question. Also, due to the size of my study, my vectors are on the order of 50*10^6 (at least before they get cut down). Due to their size, I am mostly concerned with computational efficiency and storage requirements (I am working with 16gb RAM).
So I have param1(b)..param16(b) that I have been creating histograms for. Each of these vectors is the same size and has some meaning. Additionally, right now I am concerned with the maximum of say param1(b) and then I would like to apply that subscript to param2(b)..param16(b) in an efficient manner to see what the values were. Finally, I throw it in a table for easy reading.
So I have to write this out 16x or 32x using 2 lines of code. If I decide to include the minimum, as well, then I would be looking at 32x or 64x.
I guess the short of my rambling is that is there an efficiency reason for writing it in two lines be it computational or practical? It seems that writing the function is best for my case, but I may not be thinking of something important.
I think you're data storage scheme here is what's making it difficult, see this:
If you use a standard array, or cell array, you could use those two-three lines in a loop, looping over columns of the array and the total will be about 6 lines long.
Do you have a minimal working example with random vectors and say 2-3 of them?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!