Combining unique values and counting
2 views (last 30 days)
Show older comments
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 Comments
Jan
on 5 Sep 2012
The relation between the inputs and the outputs is not clear. Please add the required details.
Accepted Answer
Andrei Bobrov
on 5 Sep 2012
Edited: Andrei Bobrov
on 5 Sep 2012
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 Comments
More Answers (1)
Azzi Abdelmalek
on 5 Sep 2012
Edited: Azzi Abdelmalek
on 5 Sep 2012
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 Comments
See Also
Categories
Find more on Introduction to Installation and Licensing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!