MATLAB Answers

Finding indexes of duplicate words in an array

17 views (last 30 days)
Brandon1363 on 21 Jun 2016
Edited: Brandon1363 on 24 Jun 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?

Accepted Answer

Walter Roberson
Walter Roberson on 21 Jun 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
Brandon1363 on 22 Jun 2016
I've figured it out now. Thanks Walter!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!