Converting strcat to use with categoricals

조회 수: 1 (최근 30일)
Andreas Brinch Nielsen
Andreas Brinch Nielsen 2014년 11월 25일
댓글: Andreas Brinch Nielsen 2014년 11월 26일
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 CenterFile Exchange에서 Fit Postprocessing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by