Finding non-unique values in an array.
181 views (last 30 days)
Show older comments
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!
1 Comment
Accepted Answer
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
on 4 Feb 2015
The histc equivalent would be:
[count,idxcount] = histc(idxc,1:numel(idxu))
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));
More Answers (1)
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"... :)
0 Comments
See Also
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!