If I have an N-by-1 table colum how is it possible to detect if any of the rows is identical?
1 Answer
If you simply want to determine if there are duplicate rows, you can use unique
to do this. You can check the number of unique values in the column and compare this to the total number of elements (numel
) in the same column
tf = unique(t.Column) == numel(t.Column)
If you want to determine which rows are duplicates, you can again use unique
but use the third output and then use accumarray
to count the number of occurrences of each value and then select those values which appear more than once.
[vals, ~, inds] = unique(t.Column, 'stable');
repeats = vals(accumarray(inds, 1) > 1);
% And to print them out:
fprintf('Duplicate value: %s\n', repeats{:})
If you want a logical vector of true
/false
for where the duplicates exist you can do something similar to that above
[vals, ~, inds] = unique(t.Column, 'stable');
result = ismember(inds, find(accumarray(inds, 1) > 1));
Or
[vals, ~, inds] = unique(t.Column, 'stable');
result = sum(bsxfun(@eq, inds, inds.'), 2) > 1;
Update
You can combine the two approaches above to accomplish what you want.
[vals, ~, inds] = unique(t.Column, 'stable');
repeats = vals(accumarray(inds, 1) > 1);
hasDupes = numel(repeats) > 0;
if hasDupes
for k = 1:numel(repeats)
fprintf('Duplicate value: %s\n', repeats{k});
fprintf(' Found at: ');
fprintf('%d ', find(strcmp(repeats{k}, t.Column)));
fprintf('\n');
end
end
-
If I use this code: [vals, ~, inds] = unique(t.Column, 'stable'); repeats = vals(accumarray(inds, 1) > 1); it returns, the values, which is duplicated, is it possible to write it in a for loop, so I can make matlab print an error message telling in which line there is an identical value and as output give a logical vector with 1 for each unique value, and 0 for each duplicated value?– RyanApr 12, 2017 at 13:38
-
It's almost what I need, Sorry for keep asking I'm really not good at programming, but now it gives me a logical vector with 1 everytime There is an Identical value and that is not quite what I want it to do. I have a csv file, and then I need to detect if any of the numbers in the first colum is identical, if that's the case, I need to print an error message telling in which line there is an identical value, and then remove the duplicated, and keep the original– RyanApr 12, 2017 at 13:56
-
The only reason I can think of doing it, is using a for loop, looping over each colum and then use an if-statement, I just don't know how to write it– RyanApr 12, 2017 at 13:56
-
It is exatly what I wanted thanks for taking the time to help, I really appreciate it! I just have one more small request, is it possible to make it print in which lines there is a duplicated value along with the value its already printing, and making it to a logical vector instead of printing the unique values?– RyanApr 12, 2017 at 14:13
-
Last question can vals be made to a logcal vectors consisting of 1 for each unique value and 0 for each duplicated instead of printing the value?– RyanApr 12, 2017 at 14:22