Info

此问题已关闭。 请重新打开它进行编辑或回答。

Rearranging array that represents a tour

1 次查看(过去 30 天)
Berlibur
Berlibur 2018-10-5
关闭: MATLAB Answer Bot 2021-8-20
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 个评论
Adam
Adam 2018-10-5
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.
Berlibur
Berlibur 2018-10-5
Yes, good point! I already feel that my proposed 'strategy' is a bit laborious, and this is a reason why.
Thank you

回答(0 个)

此问题已关闭。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by