Creating new vector that only has duplicates from original vector.
Show older comments
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 Comments
Please do not edit your question and remove the text. This is very rude.
Rena Berman
on 24 Jan 2017
(Answers dev) Restored question.
Answers (1)
Thorsten
on 7 May 2015
[ur1 a b] = unique(r1);
r2 = r1(ismember(r1, ur1(find(histc(b, unique(b)) == 2))));
2 Comments
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);
Thorsten
on 8 May 2015
r5 = [r4(r4 == r3(1)) r3];
Categories
Find more on Shifting and Sorting Matrices 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!