Equal elements in table

5 views (last 30 days)
Sine  Palm
Sine Palm on 20 Nov 2015
Commented: Walter Roberson on 20 Nov 2015
Hey. I have a table with 2 columns strings and the rest columns as numbers. How can I determine if any of the elements in the first column is the same and display in which two (or more) lines, this happens?
Hope someone can help me!

Answers (1)

Walter Roberson
Walter Roberson on 20 Nov 2015
col1 = YourTable{:,1};
[unique_col1, ~, idx] = unique(col1);
if length(unique_col1) == length(col1)
%there were no duplicates
description_of_duplicates = cell(0,2);
else
dup_table = accumarray(idx(:), (1:length(idx)).', [], @(C) {C.'});
mask = cellfun(@length, dup_table) > 1;
dup_col1 = col1(mask);
if ~iscell(dup_col1); dup_col1 = num2cell(dup_col1); end
description_of_duplicates = [dup_col1(:), dup_table(mask)];
end
Now description_of_duplicates is an N x 2 cell array. In each row, the first entry contains the content that was duplicated, and the second entry contains a row vector of the row numbers that duplicate the corresponding content.
The code should, I think, work with numeric entries and work with string entries.
  2 Comments
Sine  Palm
Sine Palm on 20 Nov 2015
It will not work for me :/
Walter Roberson
Walter Roberson on 20 Nov 2015
Have you considered the possibility of showing your code and showing the exact error message you get, or of describing the difference between what you need and the result this code gives you?

Sign in to comment.

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!