Ranking rows in a matrix without changing order of elements in rows
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
J OBrien
el 16 de Feb. de 2016
Comentada: J OBrien
el 16 de Feb. de 2016
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 comentarios
Respuesta aceptada
MHN
el 16 de Feb. de 2016
Editada: MHN
el 16 de Feb. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Genetic Algorithm en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!