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

1 view (last 30 days)
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

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 18 Apr 2014
Edited: Azzi Abdelmalek on 18 Apr 2014
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))]
  3 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!