Find at least 5 consecutive values above a certain threshold in a vector?

9 ビュー (過去 30 日間)
HYZ
HYZ 2019 年 4 月 17 日
コメント済み: Ahmad Bayhaqi 2021 年 5 月 1 日
I have a vector
M = [3,3,5,7,8,8,9,7,4,3,2,1,7,6,5,2,2,2];
I want to find at least 5 consecutive values which are above the threshold, which is 4. So the vector will be
N = [5,7,8,8,9,7]
Could anyone help me with this? Thanks.

採用された回答

Walter Roberson
Walter Roberson 2019 年 4 月 17 日
mask = M > 4;
starts = strfind([0 mask], [0 1 1 1 1 1]);
stops = strfind([mask 0], [1 1 1 1 1 0]);
N = M(starts(1) : stops(1))
  2 件のコメント
HYZ
HYZ 2019 年 4 月 17 日
How can I modify this code to get N = [5 7 8 8 9 7]? I guess the code you provided gave only the first and last value.
>> mask = M > 4;
starts = strfind([0 mask], [0 1 1 1 1 1]);
stops = strfind([mask 0], [1 1 1 1 1 0]);
N = M(starts(1) : stops(1))
N =
5 7
Walter Roberson
Walter Roberson 2019 年 4 月 17 日
stops = strfind([mask 0], [1 1 1 1 1 0]) + 4;

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

その他の回答 (2 件)

Akira Agata
Akira Agata 2019 年 4 月 17 日
If there are 2 or more consective values above the threshold, the following is one possible solution.
(* The following needs Image Processing Toolbox)
M = [3,3,5,7,8,8,9,7,4,3,6,5,8,5,8,2,1,7,6,2,6,2];
idx = M > 4;
idx = bwareafilt(idx,[5,Inf]);
label = bwlabel(idx);
N = cell(max(label),1);
for kk = 1:max(label)
N{kk} = M(label == kk);
end
Result:
>> N
N =
2×1 cell array
{1×6 double}
{1×5 double}
>> N{1}
ans =
5 7 8 8 9 7
>> N{2}
ans =
6 5 8 5 8
>>
  9 件のコメント
Akira Agata
Akira Agata 2021 年 4 月 9 日
OK. I believe the error was due to the difference between the 3D matrix in my sample code and the real data.
Is it possible to upload part of your real data and the modified code which generates the error?
Ahmad Bayhaqi
Ahmad Bayhaqi 2021 年 5 月 1 日
Thank you for your code. It works for me.
However, since my temperature data contains date. How the result of looping includes the datetime to define when the temperature is higher than threshold?
Thank you very much

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


KSSV
KSSV 2019 年 4 月 17 日
N(N>4)
  1 件のコメント
HYZ
HYZ 2019 年 4 月 17 日
N is the vector I want after thresholding and the values must be consecutive for at least 5 times.

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

カテゴリ

Help Center および File ExchangeImage Data Workflows についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by