MATLAB Answers

How to delete duplicates and replace values in an array?

4 views (last 30 days)
Hey, I'm a little stumped on my personal project. I'm trying to have MatLab check a list of ingredients, delete duplicates, and rewrite the remaining ingredient to show how many times its been mentioned. Here's an example:
[("White Onion"),("Potato"),("White Onion"),("Fruit")] -> [("2x White Onion"),("Potato"),("Fruit")]
I know unique() is a command I could use, but how would I rewrite that individual value?

Accepted Answer

Simon Chan
Simon Chan on 20 Aug 2021
Edited: Simon Chan on 20 Aug 2021
Create a table and use function groupsummary
Item=["White Onion";"Potato";"White Onion";"Fruit"];
G = groupsummary(T,"Ingredients","sum")
G =
3×2 table
Ingredients GroupCount
_____________ __________
"Fruit" 1
"Potato" 1
"White Onion" 2
Simon Chan
Simon Chan on 20 Aug 2021
Thanks Cris, forgot it is string data.
Use function writetable to put it in a text file

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!