Find index where condition is true, or end of matrix

42 Ansichten (letzte 30 Tage)
Bob Thompson
Bob Thompson am 5 Mai 2021
Beantwortet: Bob Thompson am 5 Mai 2021
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 Kommentar
Mario Malic
Mario Malic am 5 Mai 2021
Bearbeitet: Mario Malic am 5 Mai 2021
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

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

Weitere Antworten (1)

Jonas
Jonas am 5 Mai 2021
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 Kommentar
Paul Hoffrichter
Paul Hoffrichter am 5 Mai 2021
But how does the program know that this added last entry is phony data? An if-statement still appears to be required.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by