See sequences in array

1 view (last 30 days)
Rui Carapinha
Rui Carapinha on 20 Jul 2018
Answered: Guillaume on 20 Jul 2018
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 ?
  1 Comment
Guillaume
Guillaume on 20 Jul 2018
Please use proper matlab syntax in your example:
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]

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 20 Jul 2018
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

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!