Finding duplicates without using the unique function

11 次查看(过去 30 天)
I'm struggling to make a user defined function that detects duplicates within a matrix. This is what I have so far:
function bmatch = testing(data)
edges = min(data):max(data);
[counts,values] = histcounts(data, edges);
if values(counts>=2)
bmatch = 1;
else
bmatch = 0;
end
However this doesn't detect duplicates or state the number of duplicates in a given matrix. I don't understand why.
  4 个评论
John D'Errico
John D'Errico 2023-5-2
Give an example of what you want to catch, since you can always convert a matrix into a vector. So a being a matrix is irrelevant.
Kiran Sai
Kiran Sai 2023-5-2
Something like this:
A = [ 1 2 3 4 5 6 7 8 9 10]
match_a = testing(A) % should return bmatch as 0 and make a matirx [0]
B = [ 1 1 2 3 4 5 6 5 9]
match_b = testing(B) % should return bmatch as 1 and make a matrix [1 1 5 5]

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2023-5-2
移动:Walter Roberson 2023-5-2
Watch carefully:
data = [ 1 2 3 4 5 6 7 8 9 10]
data = 1×10
1 2 3 4 5 6 7 8 9 10
edges = min(data):max(data)
edges = 1×10
1 2 3 4 5 6 7 8 9 10
[counts,values] = histcounts(data, edges)
counts = 1×9
1 1 1 1 1 1 1 1 2
values = 1×10
1 2 3 4 5 6 7 8 9 10
Notice that the final count is 2 and that the vector of counts is shorter than the number of entries in edges . Read carefully about what happens in the edge cases for histcounts
Your code also has problems if the values are not all integers, or if there are non-finite values -- or if one of the values is much larger than the others. For example your code should be able to handle testing([-1e40 1e40]) without difficulty, but your code will run out of memory.
  3 个评论
Walter Roberson
Walter Roberson 2023-5-2
You really need to be thinking more about what the code should do if there are elements that are not integers.
Walter Roberson
Walter Roberson 2023-5-2
hint: if you sort the elements, then in the case where there are no duplicates, then there are no adjacent elements that are equal, but in the case that there are duplicates then there will be places where the adjacent elements are equal.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by