Finding non-unique values in an array.

174 Ansichten (letzte 30 Tage)
Shaun
Shaun am 4 Feb. 2015
Bearbeitet: dpb am 4 Feb. 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 Kommentar
Sean de Wolski
Sean de Wolski am 4 Feb. 2015
This should be a model for a well asked question +1!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

dpb
dpb am 4 Feb. 2015
Bearbeitet: dpb am 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"... :)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by