How to do this below?

1 Ansicht (letzte 30 Tage)
Mibang
Mibang am 6 Feb. 2024
Kommentiert: Voss am 6 Feb. 2024
Suppose you have a binary sequence:
a = [0 0 1 1 1 0 0 1 1 1 1 0 0 0];
And, how can you abtain a sequence b from a?
b = [0 0 3 0 0 0 0 4 0 0 0 0 0 0];
That is, place the sum of the consequtive ones at the first location of the consecutive ones.
And, make zeros for all the other locations of ones.
Thank you,

Akzeptierte Antwort

Voss
Voss am 6 Feb. 2024
One way:
a = [0 0 1 1 1 0 0 1 1 1 1 0 0 0]
a = 1x14
0 0 1 1 1 0 0 1 1 1 1 0 0 0
b = zeros(1,numel(a));
s_idx = strfind([0 a],[0 1]);
e_idx = strfind([a 0],[1 0]);
b(s_idx) = e_idx-s_idx+1;
b
b = 1x14
0 0 3 0 0 0 0 4 0 0 0 0 0 0
  2 Kommentare
Mibang
Mibang am 6 Feb. 2024
Genius! thanks.
Voss
Voss am 6 Feb. 2024
You're welcome!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by