MATLAB: Finding the set of unique values for another set of unique values

uniqueunique for each uniquevalue

hi everyone,
i have a dataset of which the first column contains buyers ID', the second column has a certin index, i need an output that shows what are the indeces associated with each unique buyer ID, for example: if i have a set like this:
1 3
1 3
1 4
2 5
2 4
3 1
3 7
3 7
3 7
3 9
M = [ 1,3;1,3;1,4;2,5;2,4;3,1;3,7;3,7;3,7;3,9];
i want an output in the form of
1 [3 4]
2 [5 4]
3 [1 7 9]
can anyone please help me?

Best Answer

i am looking for an array type [...] can it be filled with NaN or zeros?
NaN is probably wiser.
M = [ 1,3;1,3;1,4;2,5;2,4;3,1;3,7;3,7;3,7;3,9];
Mdedup = unique(M, 'rows'); %remove duplicate rows.
[id, ~, sub] = unique(Mdedup(:, 1)); %list of unique IDs and corresponding location
maxcount = max(accumarray(sub, 1)); %get max of histogram of IDs = width of matrix to create
result = [id, cell2mat(accumarray(sub, Mdedup(:, 2), [], @(v) {[v.', nan(1, maxcount - numel(v))]}))]