Finding indexes of duplicate words in an array
2 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
Walter Roberson
2016-6-21
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!