How to compute frequency of rows for submatrices?

1 Ansicht (letzte 30 Tage)
MRC
MRC am 8 Aug. 2014
Bearbeitet: Azzi Abdelmalek am 8 Aug. 2014
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]

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 8 Aug. 2014
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]
a=permute(reshape(D',size(D,2),3,[]),[2 1 3]);
uu=unique(D,'rows','stable');
[n,m]=size(uu);
for k=1:size(a,3)
b=a(:,:,k);
c=zeros(n,1);
jj=0;
for ii=1:n
jj=jj+1;
c(jj)=sum(ismember(b,uu(ii,:),'rows'));
end
out{k}=[uu c];
end
celldisp(out)

Weitere Antworten (1)

Azzi Abdelmalek
Azzi Abdelmalek am 8 Aug. 2014
Bearbeitet: Azzi Abdelmalek am 8 Aug. 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)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by