フィルターのクリア

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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by