我有,
labels = {'A' , 'B' , 'C'};
results =
A
A
C
.
.
B
结果
是一个1000*1
char,
我想将标签
与结果
进行比较并获得一个逻辑数组。
我终于发现了,
n = cellstr(results);
m = 'A';
strcmp(n,m)
可以,但我想循环执行,所以我不能使用 m = 'A';
,它应该是 m = labels(1);
,这是行不通的。
最佳答案
使用ismember
像这样(在您的情况下,v
就像cellstr(result)
):
>> 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
这可能有助于澄清输出:
>> v(lia)
ans =
'A' 'A' 'C' 'B' 'C' 'C' 'A'
>> labels(locb(lia))
ans =
'A' 'A' 'C' 'B' 'C' 'C' 'A'
在其他作品中,find(lia)
是 v
中也存在于 labels
中的字符的索引,并且 locb(lia)
给出这些元素的 labels
索引。
关于string - 在 MATLAB 中计算字符串单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26454428/