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

18 visualizaciones (últimos 30 días)
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!

Respuesta aceptada

madhan ravi
madhan ravi el 6 de Oct. de 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)

Más respuestas (0)

Categorías

Más información sobre Data Types en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by