フィルターのクリア

Finding indexes of duplicate words in an array

2 ビュー (過去 30 日間)
Brandon1363
Brandon1363 2016 年 6 月 21 日
編集済み: Brandon1363 2016 年 6 月 24 日
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!

採用された回答

Walter Roberson
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 件のコメント
Walter Roberson
Walter Roberson 2016 年 6 月 22 日
find(strcmp('dog',k))
Brandon1363
Brandon1363 2016 年 6 月 22 日
I've figured it out now. Thanks Walter!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by