0

I have the shortest path connecting a set of points, where the starting point A is fixed. I have the total distance as well as the pairwise distance, as well as the ordered indexes. I want to split the path: if the total distance of the path is greater than a certain value I want to cut it and start a new path and so on.

I managed to achieve it using cumsum and accummary (I am using MATLAB ) but it's a really ugly solution, I was wondering is there another way?

Dmax = max(totdistance);
th   = Dmax/4;                 % threshold
toSplit = distances;           % pairwise distance
nSplit  = cumsum(distances/th);
subs    = floor( nSplit ) + 1;
newPaths = accumarray( subs(:), (1:numel(subs)).', [], @(x) {cumsum(toSplit(x))});

This way I can also use the totcost and pairwaise cost, if provided, instead of the distance for example.

  • Please provide a [mcve] so we can verify that alternate solutions give the same result. If you *have* an MCVE which works and this is purely about modifying subjectively "ugly" code, this might be a candidate for [code review](https://codereview.stackexchange.com/) (rather than StackOverflow). – Wolfie May 30 '18 at 11:12
  • Cool, I didn't know its existence, it happens that I have a MCVE which works, should I just post it there or can I somehow move my question? Thank you for your time. – user158013 May 30 '18 at 11:19
  • Are you trying to solve the travelling salesman problem? – Ander Biguri May 30 '18 at 11:23
  • @user158013 Either modify this question to include the MCVE and a *specific issue* with the code, i.e. why it's ugly (performance, lacking functionality, edge cases which it doesn't work for etc). Or just post a new question on code review. – Wolfie May 30 '18 at 11:24
  • Kind of, my path has a fixed starting point but open end (can't be the starting point). – user158013 May 30 '18 at 11:28

0 Answers0