How to delete duplicates and replace values in an array?

5 Ansichten (letzte 30 Tage)
Colin TaylorMays
Colin TaylorMays am 20 Aug. 2021
Kommentiert: Simon Chan am 20 Aug. 2021
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?

Akzeptierte Antwort

Simon Chan
Simon Chan am 20 Aug. 2021
Bearbeitet: Simon Chan am 20 Aug. 2021
Create a table and use function groupsummary
Item=["White Onion";"Potato";"White Onion";"Fruit"];
T=table(Item,'VariableNames',{'Ingredients'});
G = groupsummary(T,"Ingredients","sum")
Result:
G =
3×2 table
Ingredients GroupCount
_____________ __________
"Fruit" 1
"Potato" 1
"White Onion" 2
  3 Kommentare
Cris LaPierre
Cris LaPierre am 20 Aug. 2021
Yes. Look at writetable
I would remove the method 'sum', as it doesn't make sense with string data.
ingrT = table(["White Onion","Potato","White Onion","Fruit"]','VariableNames',"ingredients");
groupsummary(ingrT,"ingredients")
ans = 3×2 table
ingredients GroupCount _____________ __________ "Fruit" 1 "Potato" 1 "White Onion" 2
Simon Chan
Simon Chan am 20 Aug. 2021
Thanks Cris, forgot it is string data.
Use function writetable to put it in a text file
writetable(G,'result.txt')

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by