How to compute frequency of rows for submatrices?

조회 수: 1 (최근 30일)
MRC
MRC 2014년 8월 8일
편집: Azzi Abdelmalek 2014년 8월 8일
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]

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 8월 8일
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)

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 8월 8일
편집: Azzi Abdelmalek 2014년 8월 8일
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)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by