MATLAB: I am triying to rearranging the matrix and form one as explained below

matrixmatrix arraymatrix manipulation

I would like to rearrange and form a new matrix, I have a matrix for example A=[1 2;2 16;3 5;1 12;2 6;3 7;1 5;2 7;3 6;1 7;2 10;3 18] this has repetitive 1st column "1 2 3 " I need to form a new martix with values in the 2nd row arranged corrosponding values resulting B=[1 2 12 5 7;2 16 6 7 10;3 5 7 6 18] Please help ,how this can be done? TIA!

Best Answer

Hi, Hope this below piece of code will help you.
aSize = size(A);
initialValue = A(1);
count = 1;
temp=[];
for i = 1: aSize(1)
if i >1 && A(i) == initialValue
[B] = A(count:i -1, :) ;
count = i;
temp = [temp B];
end;
end;
bSize = size(temp);
temp = [temp A(i- bSize(1) + 1:i, :)];
B = unique(temp.','rows', 'stable').';
Thanks.