Casting a number into a categorical array
7 次查看(过去 30 天)
显示 更早的评论
Hello,
I'm trying to swith my project to categorical arrays (from a cellstr plus a numeric index). There's one feature that should be insanely simple, but I can't find a simple way to do it. I'd like to be able to cast a number or char into an existing type of categorical array. In other words, given a categorical array x, I'd like to take a number vector or cellstr y, and produce a categorical array with the categories of x (in the same order), but the entries in y:
x = categorical({'one','two','three'})
y1 = [2,2,3]
y2 = {'two','two','three'}
categorical(y1, double(x), categories(x)) % works, but is super lengthy, data-type specific, and probably inefficient
categorical(y2, cellstr(char(x)), categories(x)) % doesn't even work (returns a categorical array valued [three,three,two])
The motivation is to acquire two arrays that index into the same list of categories. Any advice appreciated!
采纳的回答
Stephen23
2022-6-13
x = categorical({'one','two','three'})
c = categories(x)
y = setcats(categorical({'two','two','three'}),c)
d = categories(y)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Categorical Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!