Ranking rows in a matrix without changing order of elements in rows
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposta accettata
MHN
il 16 Feb 2016
Modificato: MHN
il 16 Feb 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.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Genetic Algorithm in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!