First of all it is really hard for me to describe the problem really good but I\'ll try.
Say that we have matrix A
A = [23 1; 45 1 78 1
You could use accumarray here:
accumarray
B = accumarray(A(:,2),A(:,1),[],@(x){x},{});
If you know that A is sorted, and that there is no missing entry from the second column, you can also use mat2cell:
A
mat2cell
counts = histc(A(:,2),unique(A(:,2))); B = mat2cell(A(:,1),counts);