Effacer les filtres
Effacer les filtres

finding the frequency of each row (2)

2 vues (au cours des 30 derniers jours)
Mnr
Mnr le 29 Mar 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.

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 29 Mar 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]

Plus de réponses (1)

Walter Roberson
Walter Roberson le 29 Mar 2014
You can get the counts using accumarray(A + 2, 1, [3 3])

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by