I have data like the following matrix:
A = [2 5 10 4 10; 2 4 5 1 2; 6 2 1 5 4];
A =
2 5 10 4 10
2 4 5 1 2
6 2 1 5 4
I would like to sort the by the last row based on the following criteria:
if the difference between the first element (in the third row) and second element (in the third row) is less than or equal to 2 — then move that column (the second in this case two columns to the right). Then do this for all columns until no two elements (of the last row) are within a difference of 2)
B =
2 5 4 10 10
2 4 1 5 2
6 2 5 1 4
Where (6-2 = 4) (2-5 = 3) (5-1 = 4) (1-4 = 3)
Ultimately difference between all elements of the last row and element next to it is greater than 2.
Any suggestions?
This is one possible solution: