필터 지우기
필터 지우기

Find index where condition is true, or end of matrix

조회 수: 34 (최근 30일)
Bob Thompson
Bob Thompson 2021년 5월 5일
답변: Bob Thompson 2021년 5월 5일
I have an array that represents the time based states of an analysis. I want to search the array for where a certain state starts, and if the state doesn't exist, then pick the final index of the array.
statestart = find(Results.state==5,1); % Find index of first time state is 5
if isempty(statestart)
statestart = numel(Results.state);
end
I know this works, but I would like to know if it's possible to get the same result without an if statement.
  댓글 수: 1
Mario Malic
Mario Malic 2021년 5월 5일
편집: Mario Malic 2021년 5월 5일
Maybe you could create a vector that contains states that are within Results.state using unique, that way you could skip on checking for non-existant states.

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

채택된 답변

Bob Thompson
Bob Thompson 2021년 5월 5일
I appreciate the input, this is the best I was able to do:
statestart = [find(Results.state==5,1) numel(Results.state)];
statestart(1)

추가 답변 (1개)

Jonas
Jonas 2021년 5월 5일
you could set the last entry to the condition you are searching for and then use your find function. this way at least one index can be found where the condition is met
  댓글 수: 1
Paul Hoffrichter
Paul Hoffrichter 2021년 5월 5일
But how does the program know that this added last entry is phony data? An if-statement still appears to be required.

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by