How to find indices of similar elements in a vector?

4 vues (au cours des 30 derniers jours)
Syed Fahad Hassan
Syed Fahad Hassan le 1 Août 2017
Commenté : Jan le 1 Août 2017
I have a vector x=[1 1 2 2 3]. I want to find out the indices of the elements that are similar. The desired output should be something like this. indices = 1,2 & 3,4

Réponses (1)

Geoff Hayes
Geoff Hayes le 1 Août 2017
Syed - try the following
x = [1 1 2 2 3];
groupedData = arrayfun(@(y)find(x == y), unique(x), 'UniformOutput',false);
In the above, we use arrayfun to loop over each unique element of x and apply the function
@(y)find(x == y)
which returns the indices of those elements of x that are identical to that unique element (the input y).

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by