Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

Rearranging array that represents a tour

1 visualización (últimos 30 días)
Berlibur
Berlibur el 5 de Oct. de 2018
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
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 comentarios
Adam
Adam el 5 de Oct. de 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.
Berlibur
Berlibur el 5 de Oct. de 2018
Yes, good point! I already feel that my proposed 'strategy' is a bit laborious, and this is a reason why.
Thank you

Respuestas (0)

La pregunta está cerrada.

Community Treasure Hunt

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

Start Hunting!

Translated by