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

1 回表示 (過去 30 日間)
Aleya Marzuki
Aleya Marzuki 2020 年 10 月 6 日
回答済み: madhan ravi 2020 年 10 月 6 日
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 Center および File ExchangeTables についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by