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

2 views (last 30 days)
sid28
sid28 on 3 Apr 2021
Commented: sid28 on 4 Apr 2021
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.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!