Finding non-unique values in an array.

186 visualizaciones (últimos 30 días)
Shaun
Shaun el 4 de Feb. de 2015
Editada: dpb el 4 de Feb. de 2015
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 comentario
Sean de Wolski
Sean de Wolski el 4 de Feb. de 2015
This should be a model for a well asked question +1!

Iniciar sesión para comentar.

Respuesta aceptada

Sean de Wolski
Sean de Wolski el 4 de Feb. de 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 comentarios
Sean de Wolski
Sean de Wolski el 4 de Feb. de 2015
The histc equivalent would be:
[count,idxcount] = histc(idxc,1:numel(idxu))
dpb
dpb el 4 de Feb. de 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));

Iniciar sesión para comentar.

Más respuestas (1)

dpb
dpb el 4 de Feb. de 2015
Editada: dpb el 4 de Feb. de 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"... :)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by