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

2 vues (au cours des 30 derniers jours)
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 commentaires
Paolo
Paolo le 2 Juin 2018
@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 le 4 Juin 2018
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).

Connectez-vous pour commenter.

Réponse acceptée

Paolo
Paolo le 2 Juin 2018
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' }

Plus de réponses (0)

Catégories

En savoir plus sur Categorical Arrays dans Help Center et File Exchange

Tags

Produits


Version

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by