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

2 visualizaciones (últimos 30 días)
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 comentarios
Paolo
Paolo el 2 de Jun. de 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 el 4 de Jun. de 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).

Iniciar sesión para comentar.

Respuesta aceptada

Paolo
Paolo el 2 de Jun. de 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' }

Más respuestas (0)

Categorías

Más información sobre Categorical Arrays en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by