I have a cell array like
A = {'hello'; 2; 3; 4; 'hello';2;'hello'}
I would like to find if there are repetitions in this array and identify their names and indexes. In this example I would like to have something like:
names = {'hello';2};
indexes = [1, 5, 7;
2, 6, 0];
I've put the last element of the second row of index to 0 just to not have problems in the dimensions...My problem is that the cell array is both char and double...I don't know how to deal with this...
It's messy, but it can be done:
m = max(cellfun(@length, A));
A2 = cellfun(@(e) [double(e) inf(1,m-length(e)) ischar(e)], A, 'uni' ,false);
A2 = cell2mat(A2);
[~, ~, jj] = unique(A2,'rows');
num = accumarray(jj,1,[],@numel);
[~, kk] = max(bsxfun(@eq, jj, find(num>1).'));
names = A(kk);
indices = arrayfun(@(n) find(jj==jj(kk(n))), 1:length(kk), 'uni', false);
How this works: A2
is just A
converted to a matrix of numbers. Each row represents one entry of A
, with the last column used as a flag to distinguish original numbers from original strings, and inf
used as a filler. Then the usual couple of unique
and accumarray
do the actual job, and the results are obtained from jj
and num
with some comparisons and indexing.