You can get the fourth column by manipulating the second and third outputs of unique:
[C,IA,IC] = unique(A,'rows');
counts = sum(bsxfun(@eq,IC,IA.')).';
C = [C counts(IA)]
Or if you also use the 'stable'
option of unique
, then the counts do not need to be reordered (C = [C counts]
).
OR you can use my favorite function, accumarray
:
C = [C accumarray(IC,1)]