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
Jan 2012-9-5
The relation between the inputs and the outputs is not clear. Please add the required details.
kash
kash 2012-9-5
result=
{6x2 cell}
{5x2 cell}
{4x2 cell}
{3x2 cell}
in in result{1,1} i have
'Pr1' 'P'
'Par2' 'PSO'
'Par3' 'MPSO'
'Par4' 'SOP'
'Par5' 'MPSO'
'Par6' 'SOPM'
'Par7' 'SOP'
now i want to count unique values in second colummn
so in result1{1,1} i need as
result1{1,1}(combining unique values and counting)
'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)

请先登录,再进行评论。

采纳的回答

Andrei Bobrov
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

更多回答(1 个)

Azzi Abdelmalek
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{:}

类别

Help CenterFile 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!

Translated by