making sub arrays of first column depending on the second column number

1 vue (au cours des 30 derniers jours)
Sho Gan
Sho Gan le 9 Juil 2019
Modifié(e) : Sho Gan le 9 Juil 2019
Hi,
I have a matrix with 2 columns let's assume [ 15 2;14 2;10 3;13 3;9 3] how can I get sub arrays from the first column that corresponds to the same number of second column. in this example [15 14] and [10 13 9]? Appreciate your help. Thanks.

Réponse acceptée

madhan ravi
madhan ravi le 9 Juil 2019
Wanted = splitapply(@(x){x},a(:,1),findgroups(a(:,2))) % where a is your matrix
celldisp(Wanted)
  2 commentaires
madhan ravi
madhan ravi le 9 Juil 2019
% For older versions:
w = accumarray(a(:,2),a(:,1),[],@(x){x});
Wanted = w(~cellfun('isempty',w));
celldisp(Wanted)
Sho Gan
Sho Gan le 9 Juil 2019
worked. Thank you very much.

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 9 Juil 2019
G = findgroups(YourMatrix(:,2));
subarrays = splitapply(@(varargin) {varargin{:}}, YourMatrix(:,1), G);
  1 commentaire
Sho Gan
Sho Gan le 9 Juil 2019
Modifié(e) : Sho Gan le 9 Juil 2019
Thanks so much. this worked perfectly :)

Connectez-vous pour commenter.

Catégories

En savoir plus sur Word games dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by