Filter löschen
Filter löschen

See sequences in array

1 Ansicht (letzte 30 Tage)
Rui Carapinha
Rui Carapinha am 20 Jul. 2018
Beantwortet: Guillaume am 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 Kommentar
Guillaume
Guillaume am 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]

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Guillaume
Guillaume am 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

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by