Repeat value/variable in one column based on variable in another column
1 view (last 30 days)
Show older comments
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!
0 Comments
Accepted Answer
madhan ravi
on 6 Oct 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)
0 Comments
More Answers (0)
See Also
Categories
Find more on Tables 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!