MATLAB Answers

JamJan
0

Find Borders and their Indices

Asked by JamJan
on 21 May 2019
Latest activity Commented on by Stephen Cobeldick on 22 May 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 Comments

Sign in to comment.

Tags

3 Answers

Answer by Stephen Cobeldick on 21 May 2019
Edited by Stephen Cobeldick on 21 May 2019
 Accepted Answer

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 Comments

Is it also possible to set a threshold downwards, for instance smaller then 5?
"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

Sign in to comment.


Answer by KALYAN ACHARJYA on 21 May 2019

so the first and the last zero
idx_1st0=find(A==0,1,'first');
idx_Last0=find(A==0,1,'last')

  0 Comments

Sign in to comment.


Answer by Alex Mcaulley on 21 May 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 Comments

Sign in to comment.