From: Rodrigo on
Hello,

I've been trying to come up with some code for finding a specific element adress of a given cell vector without looping through each element. For example,

L = {'L','L','L','L','L1','L2','L','L'}';
r = someFcn(L);

I want the position of L2, like r=6 (line 6 and colun 1). I am trying to find if there is a Matlab function that does this, or if I need to write my own.

Thanks
From: Walter Roberson on
Rodrigo wrote:

> I've been trying to come up with some code for finding a specific
> element adress of a given cell vector without looping through each
> element. For example,
>
> L = {'L','L','L','L','L1','L2','L','L'}';
> r = someFcn(L);
>
> I want the position of L2, like r=6 (line 6 and colun 1). I am trying
> to find if there is a Matlab function that does this, or if I need to
> write my own.

[tf, r] = ismember('L2', L);
From: Rodrigo on
"Rodrigo " <rodrigoandriolo(a)hotmail.com> wrote in message <huusj1$6r4$1(a)fred.mathworks.com>...
> Hello,
>
> I've been trying to come up with some code for finding a specific element adress of a given cell vector without looping through each element. For example,
>
> L = {'L','L','L','L','L1','L2','L','L'}';
> r = someFcn(L);
>
> I want the position of L2, like r=6 (line 6 and colun 1). I am trying to find if there is a Matlab function that does this, or if I need to write my own.
>
> Thanks

Thank you, :D
From: Jan Simon on
Dear Rodrigo!

> L = {'L','L','L','L','L1','L2','L','L'}';
> r = someFcn(L);
>
> I want the position of L2, like r=6 (line 6 and colun 1). I am trying to find if there is a Matlab function that does this, or if I need to write my own.

If the cell elements are strings, STRCMP is much faster than ISMEMBER:
r = find(strcmp(L, 'L2'));
If logical indexing can solve your problem, avoid the FIND to save further time, e.g. to remove these cell elements:
L = {'L','L','L','L','L1','L2','L','L'}';
L(strcmp(L, 'L2')) = []

Good luck, Jan
 | 
Pages: 1
Prev: Move 3d object with wiimote..
Next: bode plot