Detecting a particular word in a cell vector

Dear all,
I have a matrix whose first column is :
'MAR'
' POPSIZE'
'OTHTOTAL'
' CONIZE'
' POPIZE'
'MAR'
' POPULAR SIZE'
'OTHOTAL'
' CONSIIZE'
' POPUZE'
'MAR'
'PRODGORY'
I want to detect if the above column contains the string element ‘MAR’
thanks

 採用された回答

Andrei Bobrov
Andrei Bobrov 2012 年 7 月 9 日
編集済み: Andrei Bobrov 2012 年 7 月 9 日

0 投票

eg
A = {'MAR'
' POPSIZE'
'OTHTOTAL'
' CONIZE'
' POPIZE'
'MAR'
' POPULAR SIZE'
'OTHOTAL'
' CONSIIZE'
' POPUZE'
'MAR'
'PRODGORY'}
out = any(ismember(A,'MAR'));
or
out = any(strcmp(A,'MAR'));

1 件のコメント

Jan
Jan 2012 年 7 月 9 日
STRCMP is faster.

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

その他の回答 (2 件)

Luffy
Luffy 2012 年 7 月 9 日
編集済み: Luffy 2012 年 7 月 9 日

0 投票

see if any(strcmp(M,'MAR')) works for you, if there is MAR it shows 1 else 0
Example: If M = {'MAR'
'POPSIZE'
'OTHTOTAL'
' CONIZE'
' POPIZE'
'MAR'
' POPULAR SIZE'
'OTHOTAL'
' CONSIIZE'
' POPUZE'
'MAR'
'PRODGORY'}
use strcmp(M,'MAR')
Albert Yam
Albert Yam 2012 年 7 月 9 日

0 投票

I would use a regexp. http://www.mathworks.com/help/techdoc/ref/regexp.html Perhaps with a 'match' option, depending what you want as an output.
test = { 'MAR'
' POPSIZE'
'OTHTOTAL'
' CONIZE'
' POPIZE'
'MAR'
' POPULAR SIZE'
'OTHOTAL'
' CONSIIZE'
' POPUZE'
'MAR'
'PRODGORY'};
regexp(test,'MAR')

1 件のコメント

Sabbas
Sabbas 2012 年 7 月 9 日
all of you three were correct and I thank you

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

カテゴリ

ヘルプ センター および File Exchange で Programming についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by