MATLAB: How to find adjacent identical values in a vector, flanked on either side by outliers

diff()identical values

I am trying to find adjacent identical values in a vector, since they represent artifacts in heart rate data. So imagine 1Hz sample rate and we have a steadily climbing HR, then a stop, then it jumps to where it should be. I want to identify the suspect slope and the numbers before (and sometimes after) that value, so:
HR=[136 137 138 140 140 140 140 140 140 140 145 146 145 147]
according to certain literature, a slope over 2.6 bpm change/second is suspect, so I use diff…
a=diff(HR); %grabs the differences
b=a>2.6; %shows which ones are suspect
c=find(a==0); %shows where the slope is zero
and get
a =
1 1 2 0 0 0 0 0 0 5 1 -1 2
and
b =
0 0 0 0 0 0 0 0 0 1 0 0 0
and
c =
4 5 6 7 8 9
So the 10th element in the vector is my target, and the zero difference values (elements 4-9) in between are numbers that need to be replaced.
Is there some kind of way that I can use diff here? Is there a better solution?
Any thoughts would be welcome.

Best Answer

Keep in mind the comments from the cyclist and Star Strider but to use diff you can do something like this.
HR=[136 137 138 140 140 140 140 140 140 140 145 146 160 137 138 143 143 143 143 143 143 143 145 146 145 147]
a = [diff(HR)]; %find difference
c = a==0|abs(a)>2.6; %find points that fit your criteria.
dc = diff(c);
rangec = [find(dc==1)'+1 find(dc==-1)'];
spointind = find(diff(rangec ,[],2)==1)
rjump = rangec(spointind,:);
rangec(spointind ,:)=[]
%where rangec is the index ranges by of [start finish] index per detected segment.
now some additional conditionals manipulations could be needed to specifically determine if my 'OR' of the two conditions meets everything you want and filtering for single point changes meets all possible conditionals. but its a place to start and i know i included the target index in my range but that can be extracted.