Combining unique values and counting
2 次查看(过去 30 天)
显示 更早的评论
in result{1,1}
'Pr1' 'P'
'Par2' 'PSO'
'Par3' 'MPSO'
'Par4' 'SOP'
'Par5' 'MPSO'
'Par6' 'SOPM'
'Par7' 'SOP'
Is it possible to get the result as
result{1,1}
'Par1' 'P'
'Par2' 'PSO'
'Par4' 'SOP'
'Par7' 'SOP'
'Total' 3 (PSO ,SOP are same)
'Pr1' 'P'
'Par3' 'MPSO'
'Par5' 'MPSO'
'Par6' 'SOPM'
'Total' 3 (MPSO,SOPM are same)
plese help
2 个评论
Jan
2012-9-5
The relation between the inputs and the outputs is not clear. Please add the required details.
采纳的回答
Andrei Bobrov
2012-9-5
编辑:Andrei Bobrov
2012-9-5
one way
The initial data cell array result
result={{
'Pr1' 'P'
'Par2' 'PSO'
'Par3' 'MPSO'
'Par4' 'SOP'
'Par5' 'MPSO'
'Par6' 'SOPM'
'Par7' 'SOP'};
{
'Pr1' 'P'
'Par2' 'POS'
'Par3' 'MPSO'
'Par4' 'SOP'
'Par5' 'MPSO'
'Par6' 'SPO'
}}
r = result;
for jj = 1:numel(r)
w = r{jj};
[b,b,b] = unique(cellfun(@sort,w(2:end,2),'un',0));
c = histc(b,1:max(b));
[i1,i1] = sort(b);
n = numel(c);
d = reshape([repmat({w(1,:)},n,1),mat2cell(w(i1+1,:),c,size(w,2)),...
num2cell([repmat({'Total'},n,1) num2cell(c)],2)]',[],1);
r{jj} = cat(1,d{:});
end
3 个评论
更多回答(1 个)
Azzi Abdelmalek
2012-9-5
编辑:Azzi Abdelmalek
2012-9-5
A={'Pr1' 'P'
'Par2' 'PSO'
'Par3' 'MPSO'
'Par4' 'SOP'
'Par5' 'MPSO'
'Par6' 'SOPM'
'Par7' 'SOP'}
c=cellfun(@(x) sort(x),A(2:end,2),'uni',false)
d=unique(sort(c))
for k=1:numel(d)
B=A(2:end,:);
B=B(cellfun(@(x) all(ismember(x,d(k))),c),:)
n=size(B,1);
result=[A(1,:) ;B;{'total' , num2str(n)}]
out{k}=result
end
out{:}
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!