 I look forward to seeing them.
Get the MATLAB code
Published with MATLAB® 7.6
32 CommentsOldest to Newest
I definitely prefer the use of logical index, because the it’s more explicit. I think the code stay more readable. It’s something that I miss on other lenguajes.
It depends on the algorithm whether logical or numerical indexing is most suitable.
When I need a list of indices several times because I don’t want to create new variables, a list of numerical indices is shorter than a logical matrix. Also, when the positions of selected elements are of interest, a list of numerical indices is better, because subscripts are easy to calculate from numerical indices.
In other cases there is the warning “logical indexing is usually faster than find”. Then I use logical indexing.
Personally, I have started to use logical indexing as much as possible. What I like about logical indexing is that I can plot the data and the mask in the same plot and clearly see what I am removing and what I am not removing.
Thanks to all of you. For a long time, it seemed like people weren’t learning about logical indexing but now people are. I think that’s good
(but, as OkinawaDolphin says, not always optimal when the indices need to hang around for several operations and the array is large compared to the number of values of interest).
nowadays I do use logical indexing (with the exception that OkinawaDolphin pointed out). But the ONE thing which prevented me from using logical indexing from the beginning is the following.
When I saw the result of the following operations
> a = [1 2 3 4 5]
> i1 = (a > 3)
0 0 0 1 1
and when I realized that I can use this array in the following way (for logical indexing):
I thought I can create the logical arrays by hand and use them:
> i2 = [0 0 0 1 1]
??? Subscript indices must be real positive integers or logicals.
Of course, now I know that i2 created that way is real array which cannot be used as a mask and that the right way is
Trivial. But for me, this was the reason why I tended to use indices and ‘find’ instead of logicals for a long time.
Thanks for clarifying that the array must be of logical type for logical indexing.
I started using logical arrays a LOT more often after heeding the warnings and recommendations issued by ‘mlint’. Initially, I didn’t really believe logical arrays were that much more efficient, but profiling my code before and after has shown direct performance improvements.
I’ve always enjoyed using array indexing of all sorts (logical or otherwise) because it can be a very compact way of expressing an operation.
Here, for example, is a “logical array” method for removing row R and column C (both scalars) from a matrix X…
How cool is that ?
…how would you do the same thing if R and C were vectors ?
Actually, to answer Loren’s original question “Which Method(s) Do You Prefer?”, I have to say “none of the above”.
I often rely on the rather naughty mix of arithmetic and logical operations:
A .* (A > (17^2)/2)
…because it doesn’t require an assignment statement and can be used inline as part of a longer expression.
For myself, generally the answer to my preference depends on what I’m going to do with the selected elements. If I’m going to be doing something that can be vectorized (is “vectorizable”?) then I try to use logical arrays. If not, I tend to need the actual indices of the selected elements. As a related question, is there any way to determine which of the following will be faster (or even a rule of thumb)?
A=A(1:5000,1:100); %Explicityly stating the number of columns
some other method?
When I’m dealing with very large data sets, this is one of the most frustrating slow points, since it can exceed the time requirements of many of the calculations.
With regard to removing rows R and columns C from a matrix, you can use:
A = reshape(1:49, 7, 7);
B = A; % for comparison
R = 1:2:7; % Rows to remove
C = 2:2:7;
B(R, :) = ;
B(:, C) = ;
A = reshape(1:49, 7, 7);
R = 1:2:7; % Rows to remove
C = 2:2:7;
B = A(setxor(R, 1:7), setxor(C, 1:7));
B = A(setdiff(1:7, R), setdiff(1:7, C));
As for the 