Remove duplicate entries in matlab

  • Context: MATLAB 
  • Thread starter Thread starter roldy
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around the problem of removing duplicate rows from a matrix in MATLAB, where the order of elements within the rows does not matter. Participants explore various methods to achieve this, including the use of the 'unique' function, while addressing the need to maintain the original order of the rows in the output.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant presents a matrix and describes their difficulty in using nested loops to identify and remove duplicate rows where the order of elements does not matter.
  • Another participant suggests using the 'unique' function from MATLAB, providing a link to the documentation.
  • A different participant shares a specific dataset and expresses that the output from 'unique' does not meet their requirements, as it does not maintain the original order of unique rows.
  • One participant proposes using the full output of 'unique' to identify non-unique indexes and suggests comparing these indexes across columns.
  • Another participant reiterates the need to maintain the order of rows and clarifies that 'unique' should not be applied to individual columns, as both columns together define a point in the matrix.

Areas of Agreement / Disagreement

Participants express differing views on the effectiveness of the 'unique' function for this specific problem. There is no consensus on a single solution, and the discussion remains unresolved regarding the best approach to remove duplicates while maintaining order.

Contextual Notes

Participants highlight the importance of considering both columns of the matrix as defining a unique point, which complicates the use of standard functions like 'unique'. There are also indications of confusion regarding the interpretation of the requirement to disregard order.

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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 3 ·
Replies
3
Views
5K
Replies
7
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
3K