MATLAB: Finding indexes of duplicate words in an array

index indexing matlab array

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!

Best Answer

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))