Re-arrange matrix row elements so that a specific condition is fulfilled

1 view (last 30 days)
Say we have matrix
A =
1 2 3
1 3 5
1 2 4
2 3 7
The task is the re-arrange the row elements so that in no row 2 elements are in same order. For example rows 1 and 2 both have elements 1 and 3 so that three is after one. We can fliplr row 2 indices 1:2 and we get row 3 1 5. This has to be done for all rows. The problem in my algorithm is that it is slow and it ends up flipping some rows 3 times and some indices are again in the same order as in some rows.. Maybe this could be done with Answer Set Programming..
I have attached a matrix of 25 rows as test data
  2 Comments
Walter Roberson
Walter Roberson on 1 May 2019
My suspicion is that in the general case you may need to backtrack. There is a temptation to start at the top and keep all the rows that work moving from top to bottom, but I have a suspicion that if you do that you might force contradiction on some of the rows that do not originally work. I suspect that you might need to deliberately dearrange some of the "good" entries.

Sign in to comment.

Answers (0)

Categories

Find more on Resizing and Reshaping Matrices 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!