finding number of a column in a cell (column that contains a specified string)

how could it be done for finding first which array of a cell is equal a specified string and in second, what is number of column that contains this string
for example:
CELL={'a','b','c','d','e','f',...}
finding 'a' in CELL?(which row and column)

 採用された回答

Fangjun Jiang
Fangjun Jiang 2011 年 9 月 21 日
CELL={'a','b','c';'d','e','f'};
[Row,Col]=ind2sub(size(CELL),find(ismember(CELL,'e')))
or
[Row,Col]=ind2sub(size(CELL),find(strcmp(CELL,'e')))

5 件のコメント

Grzegorz Knor
Grzegorz Knor 2011 年 9 月 21 日
ind2sub function is unnecessary:
[row col] = find(strcmp(CELL,'a'))
mohammad
mohammad 2011 年 9 月 21 日
great! perfect!
speed of this one is so nice for a cell 600*600
Jan
Jan 2011 年 9 月 21 日
Grzegorz Knor's find(strcmp(C, 'a')) is the fastest solution.
Fangjun Jiang
Fangjun Jiang 2011 年 9 月 21 日
@Grzegorz, Good catch!
mohammad
mohammad 2011 年 9 月 21 日
Hi Jan,
yes this the fastest that Fangjun told

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

その他の回答 (2 件)

Harry MacDowel
Harry MacDowel 2011 年 9 月 21 日
find(char(CELL)=='a')
Does that help?

4 件のコメント

mohammad
mohammad 2011 年 9 月 21 日
thanks but errors:
??? Undefined function or method 'eq' for input arguments of type 'cell'.
Harry MacDowel
Harry MacDowel 2011 年 9 月 21 日
sorry should be
find(char(CELL)=='a');
Harry MacDowel
Harry MacDowel 2011 年 9 月 21 日
I corrected the answer up there.
mohammad
mohammad 2011 年 9 月 21 日
now this error:
??? Error using ==> eq
Matrix dimensions must agree.

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

Grzegorz Knor
Grzegorz Knor 2011 年 9 月 21 日
See in documentation:
doc strfind
strfind(CELL,'a')

5 件のコメント

mohammad
mohammad 2011 年 9 月 21 日
thanks a lot
in doc it says for idx=strfind(CELL,'a');
idx{:,:} must give indices
but when in command widow i run it, doesn't give indices
its very important for me to know the string is in which row and column
Grzegorz Knor
Grzegorz Knor 2011 年 9 月 21 日
CELL={'a','b','c','d','e','f';'f','e','d','c','b','a'}
idx = strfind(CELL,'a');
[row col] = find(~cellfun(@isempty,idx))
mohammad
mohammad 2011 年 9 月 21 日
thanks
it works good, only slow for a cell 600*600
thanks a lot
Jan
Jan 2011 年 9 月 21 日
I definitely prefer Grzegorz's solution in the comment above:
find(strcmp(C, 'a'))
mohammad
mohammad 2011 年 9 月 21 日
Hi Jan
OK let me check

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

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by