MATLAB: See sequences in array

array sequence

I have this array
A = {1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1}
I want to get only the beginning and the end of the 1's sequence and get something like this
A = {1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1}
Any idea of how can I do it ?

Best Answer

A = [1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1]
A([false, all([A(1:end-2); A(2:end-1); A(3:end)] == 1), false]) = 0 %replace all 1s in between between two 1s by 0
Or since it doesn't matter if you replace a 0 by a 0:
A([false, all([A(1:end-2); A(3:end)] == 1), false]) = 0 %replace any value between two 1s by a 0