MATLAB: Finding number sequences in a vector and set them to the adjacent values

number findersequence finder

Hello,
I have a vector, which represents basically a binary signal [0,1].
if the consecutive binary numbers is smaller than x, I want to find them and set them to the opposite value.
Example:
binaryValue = [ 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0];
if I define:
x <=4 ; % for zero values;
i will have:
binaryValue_new = [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0];

Best Answer

>> V = [ 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0]
V =
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
>> D = diff([0,V,0]);
>> B = find(D>0);
>> E = find(D<0)-1;
>> X = 4;
>> for k = 1:numel(B), if (E(k)-B(k))<X, V(B(k):E(k)) = 0; end, end
>> V
V =
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0