Finding Indices of Duplicate Values

127 views (last 30 days)
Suppose, I have a variable,
a{1}=[
2 2 1 3
5 2 1 1
5 2 1 4
5 2 1 2
1 1 2 1
2 2 2 1
1 1 3 4
1 1 3 3
4 1 3 5
1 1 4 3
1 1 4 4
1 1 4 1
2 2 4 1
2 2 4 2
4 1 4 2
4 1 4 6
2 2 5 2
2 2 5 6
4 1 5 1
4 1 5 2
4 1 5 6
4 1 5 5]
How can I find the indices of duplicate values for column 3:4?
I am using Matlab R2013a. Thank you in advance.

Accepted Answer

Simon
Simon on 19 Nov 2013
Hi!
You may use "unique". The second output is the index of all unique values, e.g. in column 3. All other indices are duplicates.
  6 Comments
Jeffrey Daniels
Jeffrey Daniels on 1 Oct 2018
This is one solution but it is not complete or 100% accurate. The question asked for unique matches in columns 3 and 4. Also, this method only returns values that are repeated adjacent to each other. If you want to find all matches in all rows, I would refer you to this solution: https://www.mathworks.com/matlabcentral/answers/175086-finding-non-unique-values-in-an-array
Giuseppe Degan Di Dieco
Giuseppe Degan Di Dieco on 29 Jun 2021
Thank you Guys!
So helpful your tips.
Best.

Sign in to comment.

More Answers (1)

B Mohandes
B Mohandes on 10 Nov 2016
by coincidence i found another way to do it, but thanks to those who answered already. the outputs of the command "unique" can be tricky to deal with, hence, here is a straight forward way.
>> find(hist(a,unique(a))>1)
the command (hist) counts the frequency (number of repetitions) of a certain value in a vector. if you use: hist(a), matlab will divide the whole range of values to 10 periods, and count the repetitions of values lying within these ranges. however, if you use: hist(a,b), then the repetitions are counted against the reference (b). so when you count the occurrences of each element in (a) against the unique elements of (a), and you find the results that are >2, then you're finding the elements that occurred more than once.
regards
  4 Comments
Yavor Kamer
Yavor Kamer on 10 Sep 2019
i think you can use 1:max(ib) in your second unique
Matt Fetterman
Matt Fetterman on 2 Mar 2020
I think a has to be sorted for this to work properly

Sign in to comment.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!