Reputation: 81
I have an array A = [10 20 20 30 40 10 50];
Is there any smart way to find and locate the repeating elements of this array ?
i.e.
10: [1 6];
20: [2 3];
I tried to use unique
but I failed ...
Upvotes: 0
Views: 642
Reputation: 112659
With bsxfun
and arrayfun
:
comp = tril(bsxfun(@eq, A(:), A(:).')); %'// compare all pairs of values
ind = find(sum(comp)>1); %// find repeated values
values = A(ind);
positions = arrayfun(@(n) find(comp(:,n).'.*(1:numel(A))), ind, 'uni', 0);
This gives:
>> values
values =
10 20
>> positions{:}
ans =
1 6
ans =
2 3
Upvotes: 1
Reputation: 1164
This solution only returns the values not the indices(location) of the values.
%Your Data
A=[10 20 20 30 40 10 50];
%sorted Data
A_sorted=sort(A);
%find the duplicates
idx=find(diff(A_sorted)==0);
% the unique is needed when there are more than two duplicates.
result=unique(A_sorted(idx));
Upvotes: 0
Reputation: 38032
Here is another:
>> A = [10 20 20 30 40 10 50];
>> S = sort(A);
>> S = arrayfun(@(x) [x find(x==A)], unique(S(diff(S)==0)), 'UniformOutput', false);
>> S{:}
ans =
10 1 6
ans =
20 2 3
If you don't have or want to use arrayfun
, you can use a plain loop:
A = [10 20 20 20 30 40 10 50];
S = sort(A);
S = unique(S(diff(S)==0));
R = cell(size(S'));
for ii = 1:numel(S)
R{ii} = [S(ii) find(A==S(ii))]; end
Upvotes: 1
Reputation: 124563
Here is one solution:
% input array
A = [10 20 20 30 40 10 50];
% find unique elements (vals), and map values of A to indices (valsIdx)
[vals,~,valsIdx] = unique(A);
% group element locations by the above indices mapping
locs = accumarray(valsIdx, 1:numel(valsIdx), [], @(x){sort(x)});
% keep only values that are repeated
idx = cellfun(@numel, locs) > 1;
vals = vals(idx);
locs = locs(idx);
The result:
vals =
10 20
locs =
[2x1 double]
[2x1 double]
>> celldisp(locs)
locs{1} =
1
6
locs{2} =
2
3
Upvotes: 2