MATLAB: How to count frequency of non zero elements col wise

countsumunique

I have a matrix
A = [
1 2 3 0 0;
2 2 0 2 1;
1 0 2 3 1]
I want to count column wise frequency of occurrence of all non zero numbers, i.e. 1, 2, 3 So as to get result in form of
First Col 1 (2) 2(1) 3(0)
Second Col 1(0) 2(2) 3(0)
Third Col 1(0) 2(1) 3(1)
Forth Col 1(0) 2(1) 3(1)
Fifth Col 1(2) 2(0) 3(0)

Best Answer

>> [N,C] = hist(A,0:3);
>> Z = N(C~=0,:).'
Z =
2 1 0
0 2 0
0 1 1
0 1 1
2 0 0