finding the frequency of each row (2)

1 Ansicht (letzte 30 Tage)
Mnr
Mnr am 29 Mär. 2014
Beantwortet: Azzi Abdelmalek am 29 Mär. 2014
Hello all,
Per my previous question, I have several mx2 matrices with rows from (0,1), (-1,0), (0,1), (0,-1), (1,1), (-1,1), (1,-1),(-1,-1) and I would like to find the frequency of each of above coordinates, I used unique and hisc to give me the frequency of each row. Now, since I would like to compare different matrices, I would like for each matrix have a corresponding output of 8 rows, where each row indicates the number of times that each of above coordinates appear. In particular, if e.g (0,1) is not among the rows of my matrix, I would like to see zero frequency.
For instance, if A=[1 1; 0 1; 1 0; -1 1;-1 1;0 1;0 1]
I would like to see something like:
-1 -1 0
-1 0 0
-1 1 2
0 -1 0
0 1 3
1 -1 0
1 0 1
1 1 1
Is there a way to do it? Thanks.

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 29 Mär. 2014
B=[-1 -1
-1 0
-1 1
0 -1
0 1
1 -1
1 0
1 1]
A==[1 1; 0 1; 1 0; -1 1;-1 1;0 1;0 1]
[ii,jj,kk]=unique(A,'rows')
f1=histc(kk,1:numel(jj));
f=zeros(size(B,1),1);
idx=ismember(B,ii,'rows');
M=[B zeros(size(B,1),1)];
M(idx,:)=[ii f1]

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 29 Mär. 2014
You can get the counts using accumarray(A + 2, 1, [3 3])

Kategorien

Mehr zu Mathematics 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