kits
kits

Reputation: 607

Find elements in cell array (logical 0 or 1)

I have a cell column vector of stock symbols that are all three letters long. I need to create a column vector of the same length that returns a 1 when the symbol XYZ is present, and 0 when it is not. I have wasted 2+ hours trying to figure this out, it is very frustrating.

For example, if I have this:

XYZ
DHU
EYU
XYZ
XYZ

then I need a column vector like this:

1
0
0
1
1

I appreciate the help. Have a great weekend.

Upvotes: 2

Views: 75

Answers (2)

Divakar
Divakar

Reputation: 221514

Since all cells consist of three letters, you can convert them to character array with char() and then use bsxfun for comparisons, like so -

>> A = {'XYZ';'DHU';'EYU';'XYZ';'XYZ'};
>> all(bsxfun(@eq,char(A),'XYZ'),2)
ans =
     1
     0
     0
     1
     1

Upvotes: 2

scrappedcola
scrappedcola

Reputation: 10572

You should be able to use strcmp

s1 = 'XYX';
s2 = {'XYZ';'DHU';'EYU';'XYZ';'XYZ'};

tf = strcmp(s1,s2)

Upvotes: 3

Related Questions