How to change value in a table so the old one is no longer in the table
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Antonie Brozova
el 2 de Jun. de 2018
Comentada: Peter Perkins
el 4 de Jun. de 2018
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
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
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).
Respuesta aceptada
Paolo
el 2 de Jun. de 2018
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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Categorical Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!