Find index where condition is true, or end of matrix
42 次查看(过去 30 天)
显示 更早的评论
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
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.
采纳的回答
更多回答(1 个)
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
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 Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!