Finding indexes of duplicate words in an array

2 visualizaciones (últimos 30 días)
Brandon1363
Brandon1363 el 21 de Jun. de 2016
Editada: Brandon1363 el 24 de Jun. de 2016
Hello, how would I go about finding the indexes of repeated words in an array.
k = ['dog' 'cat' 'dog' 'lion' 'tiger'];
How would I find the indexes where dog is located?
Thanks!

Respuesta aceptada

Walter Roberson
Walter Roberson el 21 de Jun. de 2016
k = {'dog' 'cat' 'dog' 'lion' 'tiger'};
[uniquewords, ~, idx] = unique(k);
Now all the places where idx == 1 are places where uniquewords{1} occurred (it will be 'cat'), all the places where idx == 2 are places where uniquewords{2} occurred (it will be 'dog') and so on.
If you know the word you are looking for, then you can use
find(strcmp('dog',k))
  3 comentarios
Walter Roberson
Walter Roberson el 22 de Jun. de 2016
find(strcmp('dog',k))
Brandon1363
Brandon1363 el 22 de Jun. de 2016
I've figured it out now. Thanks Walter!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by