How can I find duplicate values, add them to the original, and remove the duplicates?

6 views (last 30 days)
I have a cell array where the first column is a county name, the second column is a county name, and the third column is the demand (class doubles) between the first and second column. You could essentially think of column 1 and column 2 as a pair.
In this array, there are duplicates of some of the county pairs and I want to find all those duplicates. After finding the dulpicates for a particular county pair, I want to add all the demand values from those duplicates to the original one. After adding them, I want to delete the rows with the duplicates. I want to do this for any of the duplicate pairs that might appear within the array. (*Note*: this needs to do it for a variety of duplicate pairs -> so there may be 3 duplicates between Suffolk and Erie county, but there may also be 10 duplicates between Albany and Kent county.)
I was thinking I could for loops and go through this, but the index will change if I delete rows, so I'm not quite sure how to actually go about doing this.
Thank you in advance!
  1 Comment
Sulaymon Eshkabilov
Sulaymon Eshkabilov on 3 Apr 2021
Loop based coding is inefficient in terms of computation time. In this case, you can employ logical indexing to locate duplicate values and perform other processing steps w.r.t your exercise statements.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 3 Apr 2021
or perhaps just slightly further on, to the Multiple Grouping Variables part.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!