Converting strcat to use with categoricals

1 visualización (últimos 30 días)
Andreas Brinch Nielsen
Andreas Brinch Nielsen el 25 de Nov. de 2014
Comentada: Andreas Brinch Nielsen el 26 de Nov. de 2014
Hi,
We have previously used strings for properties of various entries in tables, and have now converted them to categoricals. Before, we could have cat1 and cat2 and we could create a new merged category by concatenating the two:
tab = table;
tab.cat1 = {'true','false','false','null'}';
tab.cat2 = {'one','one','many','many'}';
tab.cat3 = strcat(tab.cat1, tab.cat2);
Is there a similar approach for categoricals? What we do now is converting back and forth from string, but that seems inefficient and slow for large tables.
tab = table;
tab.cat1 = categorical({'true','false','false','null'}');
tab.cat2 = categorical({'one','one','many','many'}');
tab.cat3 = categorical(strcat(cellstr(tab.cat1), cellstr(tab.cat2)));
Instead it would be nice if you could do something like:
tab.cat3 = catcat(tab.cat1, tab.cat2);

Respuesta aceptada

Peter Perkins
Peter Perkins el 25 de Nov. de 2014
Andreas, I think the categorical times method is you're looking for:
>> tab = table(categorical({'true','false','false','null'}',{'true' 'false'}), ...
categorical({'one','one','many','many'}'), ...
'VariableNames',{'cat1' 'cat2'})
tab =
cat1 cat2
___________ ____
true one
false one
false many
<undefined> many
>> tab.cat3 = tab.cat1 .* tab.cat2
tab =
cat1 cat2 cat3
___________ ____ ___________
true one true one
false one false one
false many false many
<undefined> many <undefined>
Hope this helps.
  1 comentario
Andreas Brinch Nielsen
Andreas Brinch Nielsen el 26 de Nov. de 2014
That was exactly what I was looking for. Awesome. Thanks!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Fit Postprocessing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by