alternate to using "find" to determine indices?

Hello,
I have three logical vectors that reflect some characteristics of a modelled set of items.
At one point, I determine a small set of items that have a certain characteristic, but not two others, using find to determine their indices:
smallList=find(iList & ~rList & ~dList); %iList, rList, and dList are logical vectors
Is there a way to determine their indices using logical indexing or something else faster than find? I've had trouble wrapping my head around how I might achieve that.
Thank you for considering, David

 Accepted Answer

"Is there a way to determine their indices using logical indexing...?"
Yes, and logical indexing is usually a better way to index than using a linear index or subscripts, and you've already discovered how to do it :)
logicalIndex = iList & ~rList & ~dList;
" I've had trouble wrapping my head around how I might achieve that."
Here's a general demo
data = rand(5,10); % random values between 0 and 1
idx = data <0.5; % logical index of values less than 0.5
data(idx) = NaN; % Replaces values < 0.5 with NaNs

3 Comments

Thank you for your response. I understand your example. I am having trouble adapting it to my situation. I have a vector of class objects and the smallList vector that I obtained with find gave me the indices of specific objects that needed various operations performed upon them. Those operations are performed one object at a time using a for loop to access each object individually.
I can obtain your idx in my situation as you have suggested, but then I have a logical vector that indicates TRUE for the particular objects I want. I don't know the indices of the TRUEs. I feel like I need to know so that I can run my loop to obtain individual thisObject for each loop iteration (and do a bunch of things with each thisObject). Sorry, I may not have said that well, but I can't quite get my thinking straight.
Your help is much appreciated! David
In that case, find() is what you should use if you're working with a loop.
linIdx = find(logicalIndex);
for i = 1:numel(linIdx)
myFunction(h(linIdx(i))); % h is your vector of class obj.
end
Depending on the complexity of the function you're applying to each object, you could use arrayfun() or cellfun() to apply the function to each element of the array.
Thanks very much. I will continue to learn! :)

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2019b

Community Treasure Hunt

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

Start Hunting!