MATLAB: How to extract unique values in one column per groups of rows in other column

unique values

I have several excel worksheets with over 6000 rows of data in excel that go across multiple columns. In column 1 I have the number corresponding to an image (goes up sequentially) and in column 2 I have the parent cell number that goes up to some maximum number which varies image to image and can be in duplicates. I need to extract the number of unique values in column 2 per image but I'm not sure how to script it in matlab.
Matrix Example to illustrate problem:
1 2
1 2
1 3
1 5
1 23
2 1
3 1
3 2
3 2
3 2
4 3
4 20
4 13
4 17
4 31
4 17
etc.
and I need an output that resembles
1 4
2 1
3 2
4 5
etc.

Best Answer

Assuming sequential integers starting from one in the first column:
>> accumarray(M(:,1),M(:,2),[],@(n)numel(unique(n)))
ans =
4
1
2
5