Repeat value/variable in one column based on variable in another column

18 Ansichten (letzte 30 Tage)
I have a data table with one specific column (consisting of characters) containing some rows with 'CS1' and 'CS2' with rows of 7s and 9s in between.
E.g ['CS1' '7' '7' '7' '7' 'CS2' '9' '9' '9' 'CS1' '7' '9' '7' '7' '7' '7' '7' 'CS1' ...]
What I want to do now is create a new column containing just CS1 and CS2 based on the column above, such that CS1 would be repeated starting from 'CS1' in the column above until the row before a new CS begins. It would then do the same thing for CS2 until the next CS and so on:
Column from above: ['CS1' '7' '7' '7' '7' 'CS2' '9' '9' '9' 'CS1' '7' '9' '7' '7' '7' '7' '7' 'CS1' ...]
New column: [CS1 CS1 CS1 CS1 CS1 CS2 CS2 CS2 CS2 CS1 CS1 CS1 CS1 CS1 CS1 CS1 CS1 CS1 ...]
Any help is appreciated, thank you!

Akzeptierte Antwort

madhan ravi
madhan ravi am 6 Okt. 2020
c = {'CS1' '7' '7' '7' '7' 'CS2' '9' '9' '9' ...
'CS1' '7' '9' '7' '7' '7' '7' '7' 'CS1'};
ix = ~isnan(str2double(c));
ix1 = diff(find([true, diff(ix) < 0, true]));
Wanted = repelem(c(~ix), ix1)

Weitere Antworten (0)

Kategorien

Mehr zu Data Types 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