MATLAB: Finding the frequency of each row (2)

MATLABmatrix

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.

Best Answer

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]