Ranking rows in a matrix without changing order of elements in rows
2 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
MHN
on 16 Feb 2016
Edited: MHN
on 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.
More Answers (0)
See Also
Categories
Find more on Genetic Algorithm in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!