MATLAB Answers

0

How to find indices of similar values in a vector?

I want to get the indices of similar values in a vector. For example, I have a vector x=[1 1 2 2 3]. The output should be something like this: indices = 1,2 & 3,4

  1 Comment

@Syed Fahad Hassan: Please post a question only once.

Sign in to comment.

1 Answer

Answer by Rob Campbell on 1 Aug 2017
Edited by Rob Campbell on 1 Aug 2017
 Accepted Answer

I'm unsure if you're looking for sequential numbers or not. The following just looks for repeats. Does it do what you want?
x = [1,1,2,2,3,4,5,6,1,2,3];
% Store in a cell array the indexes of values that repeat
reps={};
for thisU=unique(x)
f=find(x==thisU);
if length(f)>1, reps{end+1}=f; end
end

  1 Comment

Sign in to comment.