Ranking rows in a matrix without changing order of elements in rows
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
MHN
2016-2-16
编辑:MHN
2016-2-16
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Genetic Algorithm 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!