Ranking rows in a matrix without changing order of elements in rows
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi there
I have a 4x4 matrix of 1s and 0s, eg A = [1 0 0 1 0; 0 1 1 1 1; 1 0 1 1 0; 0 0 0 1 0]
In my code I am calling each row, so A(1,:),A(2,:) and so forth.
How can I rank each row according to how many 1's are in the row? Without changing the order of the elements in that row. So ideally I would like:
B = [0 1 1 1 1; 1 0 1 1 0; 1 0 0 1 0; 0 0 0 1 0]
I thought about summing each row and then sorting them, but then how would I be able to continue referencing A(1,:), A(2,:)?
Background: I am trying to code a simple genetic algorithm and so I need to crossover the rows that have the most 1's.
Thanks
0 commentaires
Réponse acceptée
MHN
le 16 Fév 2016
Modifié(e) : MHN
le 16 Fév 2016
A = [1 0 0 1 0; 0 1 1 1 1; 1 0 1 1 0; 0 0 0 1 0]
B = sum(A')';
[m,n] = sort(B);
now A(n(1),:) shows the raw with the least 1's, A(n(2),:) shows the next raw with the least 1's, and so on. By this method you havent changed the order of A's raw and you also now where is the row with the smallest number of 1's and so on.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Genetic Algorithm 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!