Reputation: 4336
I have,
labels = {'A' , 'B' , 'C'};
results =
A
A
C
.
.
B
result
is a 1000*1
char,
I want compare the the labels
with results
and get a logical array.
I finally found that,
n = cellstr(results);
m = 'A';
strcmp(n,m)
works, but I want to do it in a loop, so I cant have m = 'A';
, it should be m = labels(1);
, which won't work.
Upvotes: 2
Views: 55
Reputation: 30579
Use ismember
like this (v
is like cellstr(result)
in your case):
>> labels = {'A' , 'B' , 'C'};
>> v = cellstr(char(randi(15,20,1)+64)).' %' uni. random sample of letters from A to O
v =
Columns 1 through 10
'M' 'D' 'I' 'J' 'A' 'J' 'F' 'A' 'H' 'C'
Columns 11 through 20
'B' 'D' 'C' 'C' 'A' 'J' 'E' 'I' 'K' 'H'
>> [lia,locb] = ismember(v,labels)
lia =
Columns 1 through 12
0 0 0 0 1 0 0 1 0 1 1 0
Columns 13 through 20
1 1 1 0 0 0 0 0
locb =
Columns 1 through 12
0 0 0 0 1 0 0 1 0 3 2 0
Columns 13 through 20
3 3 1 0 0 0 0 0
This might help clarify the outputs:
>> v(lia)
ans =
'A' 'A' 'C' 'B' 'C' 'C' 'A'
>> labels(locb(lia))
ans =
'A' 'A' 'C' 'B' 'C' 'C' 'A'
In other works, find(lia)
are the indexes in v
of the characters that also exist in labels
, and locb(lia)
gives the index into labels
for those elements.
Upvotes: 2
Reputation: 29064
Try something like this:
n = cellstr(results);
for k=1:3
m = labels(k);
strcmp(n,m);
end
Upvotes: 0