MATLAB: How remove tow of three consecutive same values in a vector

arrayMATLABvector

I have two vectors like this
a=[1,2,3,1,1,1,2,3,2,3,1,1,1,1,2,3,3,3,2,2,1],
where * b * is id of * a *
b=[1,2,3,11,12,13,20,21,25,27,31,32,33,34,36,40,41,42,47,48,50]
what i want to achieve is to keep the midle element of three consecutive elements or the even element if te consecuence is more than 3 elements. i want to keep the b[4] or b[11] and b[13] also the b[16]. how to achieve this?? Can any body help?

Best Answer

a = [1,2,3,1,1,1,2,3,2,3,1,1,1,1,2,3,3,3,2,2,1];
b = [1,2,3,11,12,13,20,21,25,27,31,32,33,34,36,40,41,42,47,48,50];
c=diff(a)==0;
ii1=strfind([0 c 0],[0 1 1]);
ii2=strfind([0 c 0],[1 1 0])+1;
out=cell2mat(arrayfun(@(x,y) b(x+1:2:y),ii1,ii2,'un',0))