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

1 visualizzazione (ultimi 30 giorni)
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

Risposta accettata

Azzi Abdelmalek
Azzi Abdelmalek il 18 Apr 2014
Modificato: Azzi Abdelmalek il 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 Commenti

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by