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.

 채택된 답변

Simon
Simon 2013년 11월 19일

6 개 추천

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

Did you mean,
[c,ia,ic]=unique(curriculum{1}(:,3:4),'rows')
ic being the index of all unique values?
RDG
RDG 2013년 11월 19일
Got it!
I mean:
% indices to unique values in column 3
[~, ind] = unique(a(:, 3), 'rows');
% duplicate indices
duplicate_ind = setdiff(1:size(a, 1), ind);
% duplicate values
duplicate_value = a(duplicate_ind, 3);
Kevin Kevin
Kevin Kevin 2018년 6월 10일
This is the solution I was looking for. Thank you.
Jeffrey Daniels
Jeffrey Daniels 2018년 10월 1일
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
Thank you Guys!
So helpful your tips.
Best.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

B Mohandes
B Mohandes 2016년 11월 10일

5 개 추천

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

Robert
Robert 2018년 9월 13일
편집: Robert 2018년 9월 13일
Nice solution! hist doesn't work for cell arrays though.
But you can work like this:
[C,ia,ib]=unique(a,'rows','stable');
find(hist(ib,unique(ib))>1)
Yavor Kamer
Yavor Kamer 2019년 9월 10일
i think you can use 1:max(ib) in your second unique
Matt Fetterman
Matt Fetterman 2020년 3월 2일
I think a has to be sorted for this to work properly

댓글을 달려면 로그인하십시오.

RST
RST 2024년 5월 14일

0 개 추천

If I understand correctly, you want the row indices where the pair a{1)}(3,4) are duplicated.
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 % me!
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 % and me!
2 2 4 2
4 1 4 2 % and me!
4 1 4 6
2 2 5 2
2 2 5 6
4 1 5 1
4 1 5 2 % and me!
4 1 5 6 % and me!
4 1 5 5]
a = 1x1 cell array
{22x4 double}
[~,ixu] = unique( a{1}(:,3:4), 'rows'); % gives indices of the first of the unique rows
ixd = setdiff( 1:size(a{1},1), ixu); % (duplicate rows) = (all rows) - (unique rows)
clear ixu
ixd
ixd = 1x5
6 13 15 20 21
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% show rows that are duplicates in columns 3:4
a{1}(ixd, :)
ans = 5x4
2 2 2 1 2 2 4 1 4 1 4 2 4 1 5 2 4 1 5 6
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

RDG
2013년 11월 19일

답변:

RST
2024년 5월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by