Converting strcat to use with categoricals

1 vue (au cours des 30 derniers jours)
Andreas Brinch Nielsen
Andreas Brinch Nielsen le 25 Nov 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);

Réponse acceptée

Peter Perkins
Peter Perkins le 25 Nov 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 commentaire
Andreas Brinch Nielsen
Andreas Brinch Nielsen le 26 Nov 2014
That was exactly what I was looking for. Awesome. Thanks!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by