Find Borders and their Indices

조회 수: 5 (최근 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 CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by