MATLAB: How to rearrange an array ? (only unique numbers in a column)

MATLABrearrange array unique number

How to rearrange matrix: A = [1 1;1 4;2 3;2 7;3 5;4 2;5 6] To make it looks like: B= [1 1 4;2 3 7; 3 5 0; 4 2 0; 5 6 0]
The idea is to have only unique values in first column and if (in this example "1" and "2") value used twice to keep both numbers? Thanks

Best Answer

A = [1 1;1 4;2 3;2 7;3 5;4 2;5 6] ,
c=accumarray(A(:,1),A(:,2),[],@(x) {x})
n=max(cellfun(@numel,c));
B=[unique(A(:,1),'stable') cell2mat(cellfun(@(x) [x' zeros(1,n-numel(x)) ],c,'un',0))]