Remove duplicate entries in matlab

  • MATLAB
  • Thread starter roldy
  • Start date
  • Tags
    Matlab
In summary, the conversation discusses the need to delete duplicate rows in a matrix, where the order of the values does not matter. Various attempts at using loops to accomplish this task are mentioned, as well as the use of the unique function. The desired output is a matrix with no duplicate rows, while maintaining the original order of the rows. The solution involves using the full output of unique, doing it on each column, and comparing the indexes of the columns to ensure they are the same.
  • #1
roldy
237
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
  • #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]
 
  • #4
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.
 
  • #5
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.
 
  • #6
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.
 

1. How can I remove duplicate entries in a matrix using MATLAB?

To remove duplicate entries in a matrix in MATLAB, you can use the unique function. This function will return a new matrix with only the unique elements from the original matrix. You can also specify if you want to keep the first or last occurrence of duplicate elements.

2. Can I remove duplicate entries in a cell array using MATLAB?

Yes, you can use the unique function to remove duplicate entries in a cell array as well. However, the resulting array will be a cell array of strings, as MATLAB treats all elements in a cell array as strings.

3. What happens if I have duplicate entries in a table in MATLAB?

If you have duplicate entries in a table in MATLAB, you can use the unique function to remove them. However, this function will only work on the first column of the table. To remove duplicate entries across all columns, you can use the unique function on the table's data as a cell array.

4. Is there a way to remove duplicate entries without using the unique function?

Yes, there are other methods to remove duplicate entries in MATLAB, such as using the setdiff function or creating a logical index to filter out duplicate entries. However, the unique function is the most efficient and commonly used method.

5. Can I remove duplicate entries from a vector in MATLAB?

Yes, you can use the unique function to remove duplicate entries from a vector in MATLAB. The resulting vector will contain only the unique elements from the original vector in ascending order.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • Linear and Abstract Algebra
Replies
2
Views
422
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
352
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
569
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
Back
Top