Converting strcat to use with categoricals

2 views (last 30 days)
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);

Accepted Answer

Peter Perkins
Peter Perkins on 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.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!