MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by RDG
on 22 Oct 2011

Greetings, suppose **Column** A has these values -
7
18
27
42
**65**
49
54
**65**
78
82
87
98

Is there a way to compare the values (row by row) and search for duplicates? (I'm using Matlab R2010b)I don't want the duplicated values to be removed.

Thanks.

*No products are associated with this question.*

Answer by Jan Simon
on 22 Oct 2011

Accepted answer

A = [7 18 27 42 65 49 54 65 78 82 87 98]; [n, bin] = histc(A, unique(A)); multiple = find(n > 1); index = find(ismember(bin, multiple));

Now the values `A(index)` appear mutliple times.

Answer by the cyclist
on 22 Oct 2011

Here's a slightly different way:

X = [1 2 3 4 5 5 5 1];

uniqueX = unique(X); countOfX = hist(X,uniqueX); indexToRepeatedValue = (countOfX~=1);

repeatedValues = uniqueX(indexToRepeatedValue) numberOfAppearancesOfRepeatedValues = countOfX(indexToRepeatedValue)

Harold
on 13 Apr 2012

Is there a way that I can use this approach for a cell array. Suppose I have a 1x4 cell array and each cell contains a (x,y) co-ordinates. Can I somehow modify your solution to search for duplicate cells and get rid of them?

Answer by Hannes Greim
on 19 Jan 2013

you can use "tabulate" for cell arrays.

## 0 Comments