Find Borders and their Indices

2 ビュー (過去 30 日間)
JamJan
JamJan 2019 年 5 月 21 日
コメント済み: Stephen23 2019 年 5 月 22 日
I have an array:
A = [24.7300000000000 25.0700000000000 27.5200000000000 27.9600000000000 26.2900000000000 16.6300000000000 7.27000000000000 8.57000000000000 2.16000000000000 0 0 0 0 0 0 0 0 0 12.4800000000000 26.6400000000000 37.5800000000000 43.0200000000000 41.3300000000000 41.0800000000000 33.8800000000000 34.7700000000000 42.1200000000000 50.1200000000000 51.3000000000000 60.2900000000000 58.6200000000000 34.7700000000000 12.6900000000000 0 0 0 0 0 0 0 0 0 15.5400000000000 29.6600000000000 41.7700000000000 43.2200000000000 41.3100000000000 38.4200000000000 39.6500000000000 42.3800000000000 48.9200000000000 50.2200000000000 57.5300000000000 58.1300000000000 38.9300000000000 13.6900000000000 0 0 0 0 0 0 0 0 0 0 14.4200000000000 25.2200000000000 35.3100000000000 40.2100000000000 43.7900000000000]
As you can find in the array, I have some pieces that contain consecutive zero's. I want to identify the borders (so the first and the last zero) of each part containing these consecutive sequences. How can I do that? Preferably without a for loop, but any options are welcome!

採用された回答

Stephen23
Stephen23 2019 年 5 月 21 日
編集済み: Stephen23 2019 年 5 月 21 日
Simple and efficient using diff and find:
>> D = diff([false,A==0,false]);
>> B = find(D>0) % begin of run of zeros
B =
10 34 57
>> E = find(D<0)-1 % end of run of zeros
E =
18 42 66
  2 件のコメント
JamJan
JamJan 2019 年 5 月 22 日
Is it also possible to set a threshold downwards, for instance smaller then 5?
Stephen23
Stephen23 2019 年 5 月 22 日
"Is it also possible to set a threshold downwards, for instance smaller then 5?"
Subtract the Begin from the End indices to give the runlength-1. Compare these values against your threshold to get a logical vector:
>> X = (E-B)>5
X =
1 1 1

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

その他の回答 (2 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 5 月 21 日
so the first and the last zero
idx_1st0=find(A==0,1,'first');
idx_Last0=find(A==0,1,'last')

Alex Mcaulley
Alex Mcaulley 2019 年 5 月 21 日
Using findpeaks function:
[value,firstZeros] = findpeaks(-abs(A));
firstZeros = firstZeros(~value);
[value,lastZeros] = findpeaks(-abs(A(end:-1:1)));
lastZeros = lastZeros(~value);
lastZeros = numel(A) - lastZeros + 1;

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by