How to partition a matrix by sorting a column?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Wan-Yi Chiu
le 24 Fév 2024
Déplacé(e) : Stephen23
le 24 Fév 2024
Dear friends:
I want to parttion a matrix into two submatrix by sorting the third column:
For example, the matrix is as follows:
A= [ 73.90 123.17 1.00;
73.79 121.83 0.00;
70.64 74.46 1.00;
69.74 86.40 0.00]
I need the output as:
A1= [ 73.90 123.17 1.00;
70.64 74.46 1.00]
and
A2= [ 73.79 121.83 0.00;
69.74 86.40 0.00]
Thank you very much.
0 commentaires
Réponse acceptée
Walter Roberson
le 24 Fév 2024
A= [ 73.90 123.17 1.00;
73.79 121.83 0.00;
70.64 74.46 1.00;
69.74 86.40 0.00]
u = unique(A(:,3));
A1 = A(A(:,3)==u(2),:)
A2 = A(A(:,3)==u(1),:)
1 commentaire
Plus de réponses (1)
Voss
le 24 Fév 2024
A= [ 73.90 123.17 1.00;
73.79 121.83 0.00;
70.64 74.46 1.00;
69.74 86.40 0.00]
C = splitapply(@(x){x},A,findgroups(A(:,end)));
Cell array C contains the matrices you want, A1 and A2
C{:}
so you don't need to make them separate new variables, but you can
A1 = C{2}
A2 = C{1}
0 commentaires
Voir également
Catégories
En savoir plus sur Shifting and Sorting Matrices 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!