Converting strcat to use with categoricals

1 回表示 (過去 30 日間)
Andreas Brinch Nielsen
Andreas Brinch Nielsen 2014 年 11 月 25 日
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);

採用された回答

Peter Perkins
Peter Perkins 2014 年 11 月 25 日
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 件のコメント
Andreas Brinch Nielsen
Andreas Brinch Nielsen 2014 年 11 月 26 日
That was exactly what I was looking for. Awesome. Thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFit Postprocessing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by