필터 지우기
필터 지우기

How to find prior value to NaN without terminating the loop?

조회 수: 1 (최근 30일)
Ivan Shorokhov
Ivan Shorokhov 2015년 1월 27일
편집: Ivan Shorokhov 2015년 2월 2일
Dear all,
I have following code, which I'm using to find value before NaN. For example I have following vector N=[1 2 3 4 NaN NaN], so I'm looking for value 4, which is before NaN.
And I wondering, is there any other way how I can store only first needed value without terminating it? I have tried to make an array and extract only the first value but it didn't work:
So I simply want to find prior value to NaN without terminating the loop.
Thanks for any help,
I

채택된 답변

Star Strider
Star Strider 2015년 1월 27일
I would do this:
N=[1 2 3 4 NaN NaN];
Idx = find(isnan(N), 1, 'first')-1; % Index Of Element Before First ‘NaN’
BeforeNaN = N(Idx) % Value Of Element Before First ‘NaN’
I created ‘Idx’ separately for clarity, but the two statements could be combined as one if you want to.
The code produces:
BeforeNaN =
4
  댓글 수: 2
Ivan Shorokhov
Ivan Shorokhov 2015년 1월 27일
Dear Star Strider,
First of all thank you for helping me agian :) That is great and neat solution. Thank you one more time!
Star Strider
Star Strider 2015년 1월 27일
As always, my pleasure!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by