Finding non-unique values in an array.

217 views (last 30 days)
I have an array that I would like to extract the non-uniques rows from...
'\\campus\home\home2014...' 's-pl T2* FGRE' [1] [1] [0]
'\\campus\home\home2014...' 's-pl T2* FGRE' [1] [2] [0]
'\\campus\home\home2014...' 's-pl T2* FGRE' [1] [3] [25]
So I would like to find the rows that have non-uniques values in the fifth (last) column so I get...
'\\campus\home\home2014...' 's-pl T2* FGRE' [1] [1] [0]
'\\campus\home\home2014...' 's-pl T2* FGRE' [1] [2] [0]
Thanks in advance!

Accepted Answer

Sean de Wolski
Sean de Wolski on 4 Feb 2015
C = {'\\campus\home\home2014...' 's-pl T2* FGRE' [1] [1] [0]
'\\campus\home\home2014...' 's-pl T2* FGRE' [1] [2] [0]
'\\campus\home\home2014...' 's-pl T2* FGRE' [1] [3] [25]
'\\campus\home\home2014...' 's-pl T2* FGRE' [1] [2] [0]
'\\campus\home\home2014...' 's-pl T2* FGRE' [1] [2] [4]
'\\campus\home\home2014...' 's-pl T2* FGRE' [1] [2] [4]
};
% Unique values
[~,idxu,idxc] = unique(cell2mat(C(:,5)));
% count unique values (use histc in <=R2014b)
[count, ~, idxcount] = histcounts(idxc,numel(idxu));
% Where is greater than one occurence
idxkeep = count(idxcount)>1;
% Extract from C
C(idxkeep,:)
  3 Comments
Sean de Wolski
Sean de Wolski on 4 Feb 2015
The histc equivalent would be:
[count,idxcount] = histc(idxc,1:numel(idxu))
dpb
dpb on 4 Feb 2015
I don't have histcounts either so and didn't go look it up...the needed indices using the method w/ histc above are
idxkeep= find(bin==find(n>1));

Sign in to comment.

More Answers (1)

dpb
dpb on 4 Feb 2015
Edited: dpb on 4 Feb 2015
If the column of interest is y, then
u=unique(y); % the unique values
[n,bin]=histc(y,u); % count how many of each and where
ix1=find(n>1); % index to bin w/ more than one
Index into y is bin(ix1) for each element in ix1
idx=[];
for v=find(n>1).'
idx=[idx;find(bin==v);
end
Trial data...
>> y=[0 0 25 3 3 1].';
>> u=unique(y);
>> [n,bin]=histc(y,u);
>> for v=find(n>1).',idx=find(bin==v),end
idx =
1
2
idx =
4
5
>>
Looks correct when accumulate into the array...an accumarray expression ought to be workable but will leave as "exercise for the student"... :)

Community Treasure Hunt

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

Start Hunting!