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 Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by