Creating new vector that only has duplicates from original vector.

I need help with a small part of my code.
r1=randi(6,1,5)
rollnumber=0;
if Y==0 && rollnumber<2
rollnumber=rollnumber+1;
uniquer1=unique(r1)
countOfr1=hist(r1,uniquer1)
indexToRepeatedValue = (countOfr1~=1)
repeatedValues=uniquer1(indexToRepeatedValue)
numberOfAppearancesOfRepeatedValues=countOfr1(indexToRepeatedValue);
r1repeated=repeatedValues
r1numberofrepeated=numberOfAppearancesOfRepeatedValues
r1repeated=repeatedValues %this gives me the value of the repeated numbers
r1numberofrepeated=numberOfAppearancesOfRepeatedValues %this gives me how many times each number is repeated
Both of these are useful but I need a new vector that just has the repeated values.
The problem is I cannot figure out how to create a new vector with just the repeated values.
Example
r1=randi(6,1,5)
r1=[1 1 4 5 5] %values of r1
r2=[1 1 5 5] %the new r2 vector I need
I have read through my book and searched online and I have not been able to figure this out.
Thank you.

댓글 수: 2

Stephen23
Stephen23 2015년 5월 8일
편집: Stephen23 2015년 5월 8일
Please do not edit your question and remove the text. This is very rude.
(Answers dev) Restored question.

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

답변 (1개)

Thorsten
Thorsten 2015년 5월 7일
[ur1 a b] = unique(r1);
r2 = r1(ismember(r1, ur1(find(histc(b, unique(b)) == 2))));

댓글 수: 2

I see. Please use
[ur1 a b] = unique(r1);
r2 = r1(ismember(r1, ur1(find(histc(b, unique(b)) > 1))));
And if you need it sorted, add
r2 = sort(r2);
 r5 = [r4(r4 == r3(1)) r3];

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

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

질문:

Z
Z
2015년 5월 7일

댓글:

2017년 1월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by