Effacer les filtres
Effacer les filtres

How to return row number of an element in a char matrix?

4 vues (au cours des 30 derniers jours)
Matthias
Matthias le 27 Sep 2011
Hello, I have a small question,
I have an example matrix below with char data in it;
A = {'aa';'ab';'ac';'ad'}
When I call a notation i.e. 'ab' previously on my code, I want matlab to return the row number of 'ab', so I could move to the next row. After I searched I've found "find" command, but it looks like "find" does not work for chars (I might be wrong). I typed the following inside a larger loop;
if Notation == 'ab'
[drow,dcol] = find(A,Notation);
Notation = A(drow+1,dcol);
else....
And returns the error: ??? Undefined function or method 'find' for input arguments of type 'cell'.
Can anyone spare me the correct function name for such a task?
Thanks in advance.
Matt
PS: It can be done one by one for such a small matrix, but because of this being an example I only create a 4x1 one, the real one is 260x1, so I need an actually working algorithm.

Réponse acceptée

Jan
Jan le 27 Sep 2011
The array A is not a CHAR-matrix, but a "cell string".
A = {'aa';'ab';'ac';'ad'}
row = find(strcmp(A, 'ab'));

Plus de réponses (1)

Wayne King
Wayne King le 27 Sep 2011
Hi, one way:
A = {'aa';'ab';'ac';'ad'};
index = find(ismember(A,'ab')==1)
  2 commentaires
Matthias
Matthias le 27 Sep 2011
Thanks.
Andrei Bobrov
Andrei Bobrov le 27 Sep 2011
index = find(ismember(A,'ab'))

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by