Info
This question is closed. Reopen it to edit or answer.
Rearranging array that represents a tour
1 view (last 30 days)
Show older comments
I have an array 'tour' that is an ordered array of numbers 1,...,n. In addition I have a cell array X in which each element is a position in which to cut the tour, and a cell array Y which contains information on how to rearrange the array.
For example:
tour = 2 7 6 5 4 1 3
X = {[3 4], [1 7], [1 2]};
Y = {[5 2], [3 7], [2 6]};
So the cuts should be as follows: (last element connects to the first) 2 | 7 6 | 5 4 1 3 | Resulting in arrays:
2
7 6
5 4 1 3
And then mending the pieces together according to Y to obtain: 2 5 4 1 3 7 6
How can I implement this, or is there a smarter/more efficient way to go about this?
2 Comments
Adam
on 5 Oct 2018
X would seem to be a lot more simply encoded as a single index. whether the one immediately before the cut or immediately after doesn't really matter, your pairs in X will always be consecutive (with wrapping) I assume.
also, if you could then encode Y as just an ordering of the parts from X rather than the components therein that would be a lot easier. i.e. Y would just be a permutation whose length is equal to the number of sections you end up with from your cuts in X. In a general case this would usually be numel(X) + 1, though in the specific example you give it is just numel(X) as an edge case.
Answers (0)
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!