Finding non-unique values in an array.
189 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
Réponse acceptée
Sean de Wolski
le 4 Fév 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 commentaires
Sean de Wolski
le 4 Fév 2015
The histc equivalent would be:
[count,idxcount] = histc(idxc,1:numel(idxu))
dpb
le 4 Fév 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));
Plus de réponses (1)
dpb
le 4 Fév 2015
Modifié(e) : dpb
le 4 Fév 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 commentaires
Voir également
Catégories
En savoir plus sur Matrices and Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!