Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Remove duplicate entries in matlab

  1. Apr 13, 2012 #1
    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.
  2. jcsd
  3. Apr 13, 2012 #2
  4. Apr 13, 2012 #3
    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]
  5. Apr 14, 2012 #4


    User Avatar
    Gold Member

    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.
  6. Apr 15, 2012 #5
    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)

  7. Apr 16, 2012 #6
    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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook