MATLAB Remove duplicate entries in matlab

  • Thread starter Thread starter roldy
  • Start date Start date
  • Tags Tags
    Matlab
AI Thread Summary
To remove duplicate rows from a matrix in MATLAB where the order of elements within rows does not matter, a simple loop structure is insufficient. The recommended approach involves using the `unique` function, but it requires careful handling to maintain the original order of the first occurrences. The user needs to apply `unique` on the entire matrix and then compare the indices to ensure duplicates are identified correctly. The discussion emphasizes that both columns of the matrix should be considered together to determine if any points are identical, thus retaining the first occurrence while removing subsequent duplicates.
roldy
Messages
206
Reaction score
2
Suppose I have a matrix of values v = [1 2; 3 6; 2 1; 6 7; 4 7; 8 5; 4 7; 2 10]

I need to be able to delete the duplicate rows where order does not matter. I should get left with v = [1 2; 3 6; 6 7; 4 7; 8 5; 2 10].

I'm having the hardest time figuring out a loop structure. In my previous attempts a tried to make two nested loops both going from 1:8. I tried to compare the inner loops wth row with the outer loop's qth row. If there's a match then I make the wth row = []. This does not work, since it does not take into account different orders of the row entries.

Any help would be appreciated.
 
Physics news on Phys.org
The set of data that I'm looking at is as follows:

ep = [-24.7697910000000,-15.8191235000000;-20.6771670000000,-3.54125200000000;-12.6771670000000,20.4587480000000;-20.6771670000000,-3.54125200000000;4.32283300000000,-1.04125200000000;-12.6771670000000,20.4587480000000;4.32283300000000,-1.04125200000000;13.0196582500000,-12.0401785500000;-11.9803417500000,-14.5401785500000;13.0196582500000,-12.0401785500000;-11.9803417500000,-14.5401785500000;-24.7697910000000,-15.8191235000000;]

Using unique gives

c = [-24.7698, -15.8191; -20.6772, -3.5413; -12.6772, 20.4587; -11.9803, -14.5402; 4.3228, -1.0413; 13.0197, -12.0402]

Which is wrong for what I'm looking for. I need to remove duplicate rows as you move down the matrix. I can't see how unique can accomplish this. I need to maintain the same order (stable) while searching row-wise.

This is what I need:

c = [-24.7698, -15.8191; -20.6772, -3.5413; -12.6772, 20.4587; 4.3228, -1.0413; 13.0197, -12.0402;-11.9803, -14.5402]
 
You'd have to use the full output of unique (so you can get the indexes that aren't unique), do it on each column, then compare the indexes of the columns that aren't unique and make sure they're the same.
 
roldy said:
Using unique gives

c = [-24.7698, -15.8191; -20.6772, -3.5413; -12.6772, 20.4587; -11.9803, -14.5402; 4.3228, -1.0413; 13.0197, -12.0402]

Which is wrong for what I'm looking for. I need to remove duplicate rows as you move down the matrix. I can't see how unique can accomplish this. I need to maintain the same order (stable) while searching row-wise.

I'm sorry. When you originally wrote

"I need to be able to delete the duplicate rows where order does not matter."

I though you meant the order does not matter and I gave you the URL for unique(c,rows)

Sorry.
 
Pythagorean said:
You'd have to use the full output of unique (so you can get the indexes that aren't unique), do it on each column, then compare the indexes of the columns that aren't unique and make sure they're the same.

I'm not sure if I entirely understand your statement. Unique shouldn't be done on each column because both columns define a point. First column is x, second column is y. I need to see if any points (x,y) are the same with any other points (xi,yi) where i represents the rows under the row containing (x,y). If they are the same I need to keep the first occurrence as it appears in the original matrix and delete all other occurrences.
 
Back
Top