Combining unique values and counting

2 visualizaciones (últimos 30 días)
kash
kash el 5 de Sept. de 2012
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 comentarios
Jan
Jan el 5 de Sept. de 2012
The relation between the inputs and the outputs is not clear. Please add the required details.
kash
kash el 5 de Sept. de 2012
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)

Iniciar sesión para comentar.

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 5 de Sept. de 2012
Editada: Andrei Bobrov el 5 de Sept. de 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 comentarios
kash
kash el 5 de Sept. de 2012
??? Error using ==> cat CAT arguments dimensions are not consistent.
Andrei Bobrov
Andrei Bobrov el 5 de Sept. de 2012
Editada: Andrei Bobrov el 5 de Sept. de 2012
please see on my desktop

Iniciar sesión para comentar.

Más respuestas (1)

Azzi Abdelmalek
Azzi Abdelmalek el 5 de Sept. de 2012
Editada: Azzi Abdelmalek el 5 de Sept. de 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{:}

Categorías

Más información sobre Introduction to Installation and Licensing en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by