How can I sort through a set of tables and remove the duplicates?

1 view (last 30 days)
I am writing code that will output thousands of tables of numbers. The tables will be of varying size (all 3 columns, though, if that helps). How do I go about sorting through these and discarding duplicates?
  1 Comment
Walter Roberson
Walter Roberson on 15 May 2021
Are these numeric matrices? Are they table() objects?
Are you looking for matrices that are identical? Are you looking for entries that are identical?

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 15 May 2021
isequal() can be used to test whether two table() have the same content and same variable names.
For efficiency, you can group tables according to whether they have the same size(), and then only compare the tables that are the same size.
Put all the tables inside a cell array to make it easier to process them.

Image Analyst
Image Analyst on 15 May 2021
Can't be sure since you (unfortunately) forgot to attach your table in a .mat file with the paperclip icon, but I bet it will involve the isequal() function. Attach your table if you need more help. Now, let's say that you discovered your 500th table was exactly identical to the 137th table. Which one do you want to delete? Do the tables all have different names?
if isequal(table500, table137)
clear('table500'); % Or whichever one you want to delete from memory.
end
How are your thousands of tables stored? In a cell array?
itemsToDelete = false(1, length(ca));
for k = 1 : length(ca)
t1 = ca{k}; % Extract the kth table from the cell array.
for k2 = k + 1 : length(ca)
t2 = ca{k2} % Extract the k2th table from the cell array.
if isequal(t1, t2)
% Tables are identical. Mark the later one for deletion.
itemsToDelete(k2) = true;
end
end
end
ca(itemsToDelete) = []; % Remove those items.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!