필터 지우기
필터 지우기

alternate to using "find" to determine indices?

조회 수: 9 (최근 30일)
David W Purcell
David W Purcell 2020년 4월 1일
댓글: David W Purcell 2020년 4월 1일
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

채택된 답변

Adam Danz
Adam Danz 2020년 4월 1일
편집: Adam Danz 2020년 4월 1일
"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
Adam Danz
Adam Danz 2020년 4월 1일
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.
David W Purcell
David W Purcell 2020년 4월 1일
Thanks very much. I will continue to learn! :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Language Fundamentals에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by