How to compute frequency of rows for submatrices?
Mostrar comentarios más antiguos
I have a matrix D which is a concatenation of 4 matrices of 3 rows (breaks added for clarity). I would like to construct a matrix C reporting the unique rows within the total matrix D listed for each of the sub-matrices of D with a count of how many times they occur.
D=[1 0 1 1;
0 1 1 1;
1 1 0 1;
--------
1 1 0 1;
1 1 0 1;
0 1 1 1;
--------
1 1 1 0;
0 1 1 1;
1 0 1 1;
--------
1 0 1 1;
1 0 1 1;
1 1 0 0]
So for the above matrix, there are 5 unique rows:
1 0 1 1
0 1 1 1
1 1 0 1
1 1 1 0
1 1 0 0
So breaking those 5 rows into the 4 sub-matrices with counts of occurrence within them it would be:
C=[1 0 1 1 1;
0 1 1 1 1;
1 1 0 1 1;
1 1 1 0 0;
1 1 0 0 0;
--------
1 0 1 1 0;
0 1 1 1 1;
1 1 0 1 2;
1 1 1 0 0;
1 1 0 0 0;
----------
1 0 1 1 1;
0 1 1 1 1;
1 1 0 1 0;
1 1 1 0 1;
1 1 0 0 0;
----------
1 0 1 1 2;
0 1 1 1 0;
1 1 0 1 0;
1 1 1 0 0;
1 1 0 0 1]
Respuesta aceptada
Más respuestas (1)
Azzi Abdelmalek
el 8 de Ag. de 2014
Editada: Azzi Abdelmalek
el 8 de Ag. de 2014
With one for loop
D=[1 0 1 1; 0 1 1 1;1 1 0 1; 1 1 0 1;1 1 0 1; 0 1 1 1; 1 1 1 0; 0 1 1 1; 1 0 1 1; 1 0 1 1; 1 0 1 1; 1 1 0 0]
uu=unique(D,'rows','stable');
out=[]و
for k=1:3:size(D,1)
[v1,v2,v3]=unique(D(k:k+2,:),'rows');
g=[0; histc(v3,1:size(v1,1))];
[w1,w2]=ismember(uu,v1,'rows');
c=g(w2+1);
out{end+1}=[uu c];
end
celldisp(out)
Categorías
Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!