MATLAB: How to remove identical matrix

MATLAB: How to remove identical matrix


I have about 2000 matrices. They have the same number of columns but different number of rows. Some of these matrices are duplicates with identical number of rows, columns and identical values for all elements.
Here is my question. How do I go through these 2000 matrices and delete all duplicates. Is there a similar function like “unique” for matrices?
Many thanks!

Best Answer

  • %Create a cell array of matrices with same cols but diff rows and values
    for k = 1:5
    M{k} = 100*rand(k, 6);
    M{6} = M{1}; %Duplicate of 1
    M{7} = M{3}; %Duplicate of 3

    M{8} = M{3}; %Duplicate of 3
    Ndig = 10; %Number of digits of precision
    Mstr = cellfun(@(x) mat2str(x, Ndig), M, 'uniformoutput', false); %Convert to str representation
    [~, Midx] = unique(Mstr, 'stable'); %Find unique string
    Munq = M(Midx); %Get only the unique matrix
  • Add a Comment

    Your email address will not be published. Required fields are marked *