find for sign changes

190 ビュー (過去 30 日間)
Kugen Raj
Kugen Raj 2012 年 3 月 17 日
コメント済み: Walter Roberson 2021 年 3 月 4 日
Columns 49 through 56
0.8710 0.9483 1.0408 1.1538 1.2956 1.4796 1.7292 2.0885
Columns 57 through 64
2.6538 3.6815 6.1847 119.1838 -118.8424 -5.8436 -3.3407 -2.3137
Above is part of my results. I need to find for value that changes from positive to negative. In the above data, after 119.1838(column 60) the -118.8424(column 61) appears. I need to find this value. Hoe can I do this?

採用された回答

Walter Roberson
Walter Roberson 2012 年 3 月 17 日
find(x(1:end-1)>0 & x(2:end) < 0)
Another approach that might be close enough for your purposes:
find(diff(x>=0),1)
  4 件のコメント
Jan
Jan 2012 年 3 月 18 日
But what happens with your first approach for [1, 0, -1]?
The OP must decide this detail.
Walter Roberson
Walter Roberson 2012 年 3 月 18 日
In the case of [1, 0, -1] there is no position at which the value is positive and the next value is negative :)

サインインしてコメントする。

その他の回答 (3 件)

Jan
Jan 2012 年 3 月 17 日
Walter's method detects multiple sign changes also. This finds the first negative number only, but this meets your problem description also:
index = find(x < 0, 1, 'first')

Rasmus Herlo
Rasmus Herlo 2020 年 1 月 22 日
編集済み: Rasmus Herlo 2020 年 1 月 22 日
Finding any place where the sign changes, either from neg to pos or from pos to neg could also be done in general by this:
Idx_Change = sort([strfind(x>=0, [0 1]) strfind(x>=0, [1 0])]);
  1 件のコメント
Walter Roberson
Walter Roberson 2020 年 1 月 22 日
Yes but be careful about exact 0.

サインインしてコメントする。


Alireza Ahani
Alireza Ahani 2021 年 3 月 4 日
but when I checked with a dataset that includes zero-down-crosing event, they would be ignored, so I added some lines to his:
ZDCI = find(x(1:end-1)>0 & x(2:end) < 0); % zero down crossing index
Zero_indx = find(x == 0);
k = find( x(Zero_indx-1) > 0);
ZDCI = sort([ZDCI Zero_indx(k)]);
  1 件のコメント
Walter Roberson
Walter Roberson 2021 年 3 月 4 日
If the task is zero down crossing and zeros can be encountered, then the above code is not correct, as it does not take into account multiple zeros. For example for zero down crossing, you might have +1 followed by 5 zeros, followed by -1. The zeros could, for example, be present due to filtering of values that were close to zero, such as due to measurement noise on a sigmoid that crossed zero. Detection for such cases cannot be restricted to any fixed finite number of look-back.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by