How can I search for specific string in a cell array

5 ビュー (過去 30 日間)
Sami Rahman
Sami Rahman 2016 年 1 月 30 日
コメント済み: Sami Rahman 2016 年 1 月 30 日
I have a cell array C= 100,000x1 size. within this cell array, certain rows are the name of the months. How can I find the location of these months from C? There could be several number of months in C and the location of the months are random. Please help.

採用された回答

Walter Roberson
Walter Roberson 2016 年 1 月 30 日
Provided that C is a cell array that contains only strings,
monthnames = {'Jan', 'Feb', 'Mar', 'April', 'May', 'June', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'}; %or as appropriate
[tf, idx] = ismember(C, monthnames);
Now tf and idx will be arrays same size as C. tf(I,J) true indicates that C(I,J) matches one of the months, and for those locations, idx(I,J) tells you which of the months was matched.
If your cell array contains non-strings as well then
tf = cellfun(@(S) ischar(S) && ismember(S, monthnames));
and finding the index efficiently is trickier.
  1 件のコメント
Sami Rahman
Sami Rahman 2016 年 1 月 30 日
thanks you so much Walter Roberson. ismember did the trick!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by