Find Borders and their Indices
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Stephen23
le 21 Mai 2019
Modifié(e) : Stephen23
le 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 commentaires
Stephen23
le 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
Plus de réponses (2)
KALYAN ACHARJYA
le 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 commentaires
Alex Mcaulley
le 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 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!