Here you can use accumarray :
B = accumarray(A(:,2),A(:,1),[],@(x){x},{});
If you know that A sorted and that there is no missing entry in the second column, you can also use mat2cell :
counts = histc(A(:,2),unique(A(:,2))); B = mat2cell(A(:,1),counts);
Jonas source share