Reshaping an array of n rows in a custom way

I have an output file (txt) from a software that appears in the following form (nx2 array).
1 4.5
1 5
1 6.4
2 7
2 3.5
2 4
3 2
3 1.1
3 9
In order to use this data in another software, i want to rearrange the array in the form (nx4) so that i can get an outputfile that has the data in the following form.
1 4.5 5 6.4
2 7 3.5 4
3 2 1.1 9
I would appreciate if someone can guide me to get the data in the desired format.

 Accepted Answer

B = [unique(A(:,1)) reshape(A(:,2)',[],3)']

1 Comment

Thanks a lot for your response. It works. I can make it more flexible by using size(unique(A(:,1)),1) instead of "3" so that it works independent of required rows.

Sign in to comment.

More Answers (1)

a = [...
1.0000 4.5000
1.0000 5.0000
1.0000 6.4000
1.0000 0.6700
2.0000 7.0000
2.0000 3.5000
2.0000 4.0000
3.0000 2.0000
3.0000 1.1000
3.0000 9.0000
3.0000 7.9800
3.0000 5.9000];
jj = cell2mat(arrayfun(@(x)(1:x)',accumarray(a(:,1),1),'un',0));
out = accumarray([a(:,1),jj],a(:,2));

Categories

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!