How to compute frequency of rows for submatrices?

3 次查看(过去 30 天)
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 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by