-3

Given a vector

a=[
     1
     2
     3
     9
    12
    13
    14
    17
     8
     9
    12
    13
    14
    17];

how can we sure that an element is duplicate and use this as a condition. I want say if a have a duplicate number so...

Ahmad
  • 73
  • 3
  • 13

2 Answers2

0

To determine if there is any duplicate:

result = sum(sum(bsxfun(@eq, a, a.'))) > numel(a);

This gives true if there any duplicates and false otherwise.


Or:

result = numel(unique(a)) < numel(a);

Or:

result = any(diff(sort(a))==0);
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
0

First get the counts for each element

C = accumarray(M, 1);
counts = C(a);

Note: you can also use any of the other methods outlines in the reference below to get the number of counts.

Then flag which elements have more than 1 count

dups = counts > 1;

References

How can I count the number of elements of a given value in a matrix?

Community
  • 1
  • 1
jmetz
  • 12,144
  • 3
  • 30
  • 41