## Finding equal members in a vector

Asked by mr mo

### mr mo (view profile)

on 17 Dec 2017
Latest activity Commented on by Star Strider

### Star Strider (view profile)

on 17 Dec 2017
Hi. My question is how can I find equal members in a vector with their indices.
For example I have this vector.
V = [ 10 13 10 20 10 95 70 13];
Thanks a lot.

Image Analyst

### Image Analyst (view profile)

on 17 Dec 2017
This has nothing to do with the MATLAB compiler product or the Coder product, or a GUI or 3-D plots, or nearly all the tags you applied. Save yourself some time and only put the relevant tags that will help people.

### jean claude (view profile)

Answer by jean claude

on 17 Dec 2017

find(x==10)

mr mo

### mr mo (view profile)

on 17 Dec 2017
Assume that I don't know the values of the vector.
jean claude

### jean claude (view profile)

on 17 Dec 2017
[a] = histc(x,unique(x));
t=unique(x);
c=find(a>1);
d=0;
for i=1:length(c);
d=[d find(x==t(c(i)))];
end
d(1)=[]; % d is the output

on 17 Dec 2017
Edited by YT

on 17 Dec 2017

mr mo

### mr mo (view profile)

on 17 Dec 2017
In my question I want to have the below form of output
1 3 5
2 8
that indicates
V(1) = V(3) = V(5)
and
V(2) = V(8)
YT

### YT (view profile)

on 17 Dec 2017
Something like this then?
clear C
V = [ 10 13 10 20 10 95 70 13];
unqV = unique(V);
for i = 1:length(unqV);
C{i,1} = unqV(i)
C{i,2} = find(V==unqV(i));
end
%C = {10,[1 3 5];
% 13,[2 8];
% 20,4;
% 70,7;
% 95,6}

### Star Strider (view profile)

Answer by Star Strider

### Star Strider (view profile)

on 17 Dec 2017

If you only want the repeated values in the vector and their indices:
V = [ 10 13 10 20 10 95 70 13]; % Original Vector
[Vu,~,I] = unique(V(:), 'stable'); % Unique Values
Tally = accumarray(I, 1); % Count Occurrences
Idx = bsxfun(@eq, V, Vu(Tally > 1)) .* (1:length(V)); % Determine Indices Of Multiple Values Only
OutD = [Vu(Tally > 1) Idx] % Double Matrix Result
for k1 = 1:size(Idx,1)
IdxC{k1} = Idx(k1,(Idx(k1,:)>0)); % Keep Only Non-Zero Values
OutC{k1} = {OutD(k1,1) IdxC{k1}}; % Cell Array Result
end
celldisp(OutC) % View Cell Array (Optional)
OutD =
10 1 0 3 0 5 0 0 0
13 0 2 0 0 0 0 0 8
The ‘OutC’ cell array result eliminates the zeros in the ‘(2:end)’ columns of ‘OutD’.

Show 1 older comment
Star Strider

### Star Strider (view profile)

on 17 Dec 2017
You did not say the line that threw that error.
My code ran for me without error in R2017b, with the default ‘automatic expansion’.
Use these nested bsxfun calls instead:
Idx = bsxfun(@times, bsxfun(@eq, V, Vu(Tally > 1)), (1:length(V)));
mr mo

### mr mo (view profile)

on 17 Dec 2017
Is there any possibility to use other command except bsxfun and accumarray ?
Star Strider

### Star Strider (view profile)

on 17 Dec 2017
Not that I am aware of. It is necessary to accumarray (or one of the histogram functions) to count the occurrences. Using bsxfun is necessary to do the array calculations.

### Jos (10584) (view profile)

Answer by Jos (10584)

### Jos (10584) (view profile)

on 17 Dec 2017

V = [ 10 13 10 20 10 95 70 13];
[VU, ~, j] = unique(V)
P = accumarray(j, 1:numel(V), [] ,@(x) {x})
% P{k} holds all the indices where V equals VU(k)