delete duplicate rows in matlab in matlab

To delete duplicate rows in a matrix in Matlab, you can use the 'unique' function.

Here is the code to remove duplicate rows:

main.m
A = [1 2 3;
     4 5 6;
     1 2 3;
     7 8 9];
[~, idx, ~] = unique(A,'rows','stable'); % 'stable' parameter keeps the order of rows same as input
A_unique = A(idx,:);
170 chars
7 lines

In this code, A is your matrix. When passed to unique with the option 'rows', it returns a matrix containing the distinct rows of A. The second output idx is an index vector that tells you where each of these distinct rows first appear in the original matrix. So the last line returns only the unique rows of the original matrix A.

Note that the 'stable' option is used to maintain the order of rows. If you don't care about the order of rows in the result, you can omit this parameter.

related categories

gistlibby LogSnag