See sequences in array
1 view (last 30 days)
Show older comments
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
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]
Accepted Answer
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
0 Comments
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!