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...
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...
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);
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?