How to change value in a table so the old one is no longer in the table

2 次查看(过去 30 天)
Hi everyone, I am trying to replace values in the table pizzasize using code
ind = find(pizzasize.CrustDescription == 'MidCrust');
for i = 1 : size(ind)
pizzasize.CrustDescription(ind(i)) = 'Mid';
end
ind = find(pizzasize.CrustDescription == 'ClassicCrust');
for i = 1 : size(ind)
pizzasize.CrustDescription(ind(i)) = 'Mid';
end
ind = find(pizzasize.CrustDescription == 'ThinNCrispy');
for i = 1 : size(ind)
pizzasize.CrustDescription(ind(i)) = 'Thin';
end
ind = find(pizzasize.CrustDescription == 'ThinCrust');
for i = 1 : size(ind)
pizzasize.CrustDescription(ind(i)) = 'Thin';
end
but when I call summary(pizzasize) I get
CrustDescription: 250×1 categorical
Values:
ClassicCrust 0
DeepPan 83
MidCrust 0
ThinCrust 0
ThinNCrispy 0
Mid 85
Thin 82
How can I get rid of the values, that are no longer present in the table? Thanks for help.
  9 个评论
Paolo
Paolo 2018-6-2
@Antonie You are welcome. I have submitted an answer to the question, please accept it so it will be easily visible to other users having the same question, and for the question to be market as closed.
Peter Perkins
Peter Perkins 2018-6-4
Antonie, if I am reading your code correctly, you are combining two categories into one, and renaming the combined category. You want mergecats. It's a one-liner:
pizzasize.CrustDescription = mergecats(pizzasize.CrustDescription,{'MidCrust' 'ClassicCrust'},'Mid')
Also: all those loops were not really necessary. If mergecats didn't exist, you'd want to do this:
pizzasize.CrustDescription(pizzasize.CrustDescription == 'MidCrust') = 'Mid'
without a loop (and without a find, for that matter).

请先登录,再进行评论。

采纳的回答

Paolo
Paolo 2018-6-2
You can remove categories from categorical arrays using removecats.
View your categories with command:
categories(pizzasize.CrustDescription)
The categories:
{'ClassicCrust'}
{'DeepPan' }
{'MidCrust' }
{'ThinCrust' }
{'ThinNCrispy' }
Remove 'DeepPan' category with the command:
pizzasize.CrustDescription = removecats(pizzasize.CrustDescription,'DeepPan');
The remaining categories:
categories(pizzasize.CrustDescription)
{'ClassicCrust'}
{'MidCrust' }
{'ThinCrust' }
{'ThinNCrispy' }

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Categorical Arrays 的更多信息

标签

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by