# Find Borders and their Indices

6 Ansichten (letzte 30 Tage)
JamJan am 21 Mai 2019
Kommentiert: Stephen23 am 22 Mai 2019
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!
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Stephen23 am 21 Mai 2019
Bearbeitet: Stephen23 am 21 Mai 2019
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 KommentareKeine anzeigenKeine ausblenden
JamJan am 22 Mai 2019
Is it also possible to set a threshold downwards, for instance smaller then 5?
Stephen23 am 22 Mai 2019
"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

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (2)

KALYAN ACHARJYA am 21 Mai 2019
so the first and the last zero
idx_1st0=find(A==0,1,'first');
idx_Last0=find(A==0,1,'last')
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Alex Mcaulley am 21 Mai 2019
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;
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Performance and Memory finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by