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

18 次查看(过去 30 天)
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!

采纳的回答

madhan ravi
madhan ravi 2020-10-6
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)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Types 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by