我的部分数据(字符串单元格数组)如下所示我想计算特定字符串的出现次数(例如,'P0702'
,'P0882'
等),并以如下所示的输出形式显示出现次数的总和:
'1FA' '2012' 'F' '' '' '' '' '' 'P0702' 'P0882'
'1Fc' '2012' 'r' '' '' '' '' '' 'P0702' '' '' ''
'1FA' '2012' 'f' '' '' '' '' '' 'P0702' 'P0882' ''
'1FA' '2012' 'y' '' '' '' 'P0702' '' '' '' '' ''
'1FA' '2012' 'g' '' '' '' '' '' '' '' '' '' ''
'1FA' '2012' 'u' '' 'P0702' 'P0882' '' '' '' '' ''
'1FA' '2012' 'y' '' 'P0702' '' '' '' '' '' '' ''
'1FA' '2012' 'n' '' 'P0702' '' '' '' '' '' '' ''
'1FA' '2012' 'j' '' '' '' '' '' '' '' '' 'P0702'
'1FA' '2012' 'u' 'P0702' '' '' '' '' '' '' '' ''
'1FM' '2013' 'x' '' '' '' '' '' 'P1921' '' '' ''
'1FM' '2013' 'c' '' 'P1711' '' '' '' '' '' '' ''
'1FM' '2013' 'c' '' '' '' '' '' 'P0702' 'P0882' ''
'1FM' '2009' 'E' '' '' '' '' '' '' '' 'P0500'
输出:
sum of counts above
P0702 15
P0500 1
P1711 1
等等。
我试着使用
sum(strcmp(d,{'P0882'}),2);
来告诉我'P0882'
发生了多少次,但是很难对每个数据字符串都使用它。 最佳答案
您可以执行以下操作,基本上按照您的建议应用strcmp
,但在一个预先确定要计数的唯一字符串/数据名的循环中。
我修改了一下你提供的数据,以便尺寸合适代码是注释的,很容易理解:
C = {'1FA' '2012' 'F' '' '' '' '' '' 'P0702' 'P0882' ;
'1Fc' '2012' 'r' '' '' '' '' '' 'P0702' '';
'1FA' '2012' 'f' '' '' '' '' '' 'P0702' 'P0882';
'1FA' '2012' 'y' '' '' '' 'P0702' '' '' '';
'1FA' '2012' 'g' '' '' '' '' '' '' '';
'1FA' '2012' 'u' '' 'P0702' 'P0882' '' '' '' '' ;
'1FA' '2012' 'y' '' 'P0702' '' '' '' '' '' ;
'1FA' '2012' 'n' '' 'P0702' '' '' '' '' '' ;
'1FA' '2012' 'j' '' '' '' '' '' '' 'P0702' ;
'1FA' '2012' 'u' 'P0702' '' '' '' '' '' '' ;
'1FM' '2013' 'x' '' '' '' '' '' 'P1921' '';
'1FM' '2013' 'c' '' 'P1711' '' '' '' '' '';
'1FM' '2013' 'c' '' '' '' '' '' 'P0702' 'P0882';
'1FM' '2009' 'E' '' '' '' '' '' '' 'P0500'}
%// Find unique strings to count occurence of.
[strings,~,~] = unique(C(:,4:end));
%// Remove empty cells automatically.
strings = strings(~cellfun(@isempty,strings));
%// Initialize output cell array
Output = cell(numel(strings),2);
%// Count occurence. You can combine the 2 lines into one using concatenation.
for k = 1:numel(strings)
Output{k,1} = strings{k};
Output{k,2} = sum(sum(strcmp(C(:,4:end),strings{k})));
end
让我们用这个做一个漂亮的桌子:
T = table(Output(:,2),'RowNames',Output(:,1),'VariableNames',{'TotalOccurences'})
输出:
T =
TotalOccurences
_______________
P0500 [ 1]
P0702 [10]
P0882 [ 4]
P1711 [ 1]
P1921 [ 1]
如果您没有访问
table
函数的权限,则可以创建一个带有标题的单元格数组,并稍微更改循环:%// Initialize output cell array
Output = cell(numel(strings)+1,2);
%// Count occurence
for k = 1:numel(strings)
Output{k+1,1} = strings{k};
Output{k+1,2} = sum(sum(strcmp(C(:,4:end),strings{k})));
end
%T = table(Output(:,2),'RowNames',Output(:,1),'VariableNames',{'TotalOccurences'})
Output(1,:) = {'Data' 'Occurence'}
输出:
Output =
'Data' 'Occurence'
'P0500' [ 1]
'P0702' [ 10]
'P0882' [ 4]
'P1711' [ 1]
'P1921' [ 1]