在另一个单元格数组matlab中计算一个单元格数组中的项目
Count items in one cell array in another cell array matlab
我有 2 个元胞数组,它们是 "celldata" 和 "data"。它们都在内部存储字符串。现在我想检查 "celldata" 中的每个元素是否在 "data" 中?例如,celldata = {'AB'; 'BE'; 'BC'} 和数据={'ABCD' 'BCDE' 'ACBE' 'ADEBC '}。我希望预期的输出对于 AB 是 s=3 和 v=1,对于 BE 是 s=2 和 v=2,对于 BC 是 s=2 和 v=2,因为我只需要计算字符串的序列'celldata'
我写的代码如下所示。任何帮助都将不胜感激。
我的代码:
s=0; support counter
v=0; violate counter
SV=[]; % array to store the support
VV=[]; % array to store the violate
pairs = ['AB'; 'BE'; 'BC']
%celldata = cellstr(pairs)
celldata = {'AB'; 'BE'; 'BC'}
data={'ABCD' 'BCDE' 'ACBE' 'ADEBC '} % 3 AB, 2 BE, 2 BC
for jj=1:length(data)
for kk=1:length(celldata)
res = regexp( data(jj),celldata(kk) )
m = cell2mat(res);
e=isempty(m) % check res array is empty or not
if e == 0
s = s + 1;
SV(jj)=s;
v=v;
else
s=s;
v= v+1;
VV(jj)=v;
end
end
end
如果我对你的变量的理解正确,s
是子字符串 AB
、AE
和 BC
不出现的单元格数 v
是它执行的次数。如果这是准确的,那么
v = cellfun(@(x) length(cell2mat(strfind(data, x))), celldata);
s = numel(data) - v;
给予
v = [1;1;3];
s = [3;3;1];
我有 2 个元胞数组,它们是 "celldata" 和 "data"。它们都在内部存储字符串。现在我想检查 "celldata" 中的每个元素是否在 "data" 中?例如,celldata = {'AB'; 'BE'; 'BC'} 和数据={'ABCD' 'BCDE' 'ACBE' 'ADEBC '}。我希望预期的输出对于 AB 是 s=3 和 v=1,对于 BE 是 s=2 和 v=2,对于 BC 是 s=2 和 v=2,因为我只需要计算字符串的序列'celldata'
我写的代码如下所示。任何帮助都将不胜感激。 我的代码:
s=0; support counter
v=0; violate counter
SV=[]; % array to store the support
VV=[]; % array to store the violate
pairs = ['AB'; 'BE'; 'BC']
%celldata = cellstr(pairs)
celldata = {'AB'; 'BE'; 'BC'}
data={'ABCD' 'BCDE' 'ACBE' 'ADEBC '} % 3 AB, 2 BE, 2 BC
for jj=1:length(data)
for kk=1:length(celldata)
res = regexp( data(jj),celldata(kk) )
m = cell2mat(res);
e=isempty(m) % check res array is empty or not
if e == 0
s = s + 1;
SV(jj)=s;
v=v;
else
s=s;
v= v+1;
VV(jj)=v;
end
end
end
如果我对你的变量的理解正确,s
是子字符串 AB
、AE
和 BC
不出现的单元格数 v
是它执行的次数。如果这是准确的,那么
v = cellfun(@(x) length(cell2mat(strfind(data, x))), celldata);
s = numel(data) - v;
给予
v = [1;1;3];
s = [3;3;1];