MATLAB Answers

detect same value

8 views (last 30 days)
pink on 10 Jun 2011
how do I detect on the elements of a (:, 1) there is the same value?
a=[1 34;
2 33;
2 45;
3 32;
4 65;
4 99;
5 11;
1 33;
6 22]

Accepted Answer

Walter Roberson
Walter Roberson on 10 Jun 2011
u = unique(a(:,1));
if length(u) ~= size(a,1)
%there were duplicates
If you want to find out which are equal to which,
bsxfun(@eq, a(:,1), a(:,1).'))
and that will return a logical matrix in which for row r, column c is set if a(r) == a(c)
Walter Roberson
Walter Roberson on 6 Mar 2013
setdiff(V, unique(V)) %for vector V, results are in sorted order
T = bsxfun(@eq, V(:), V(:).'); %for vector V
sum(T) > 1 %will be true at index K if V(K) is not unique

Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!