0

In this matlab post, one can find solution of "Loop erasing random walk" vector problem. This problem consists in "erasing loops" which means: removing integers between any integer repetition.

Example:

v=[3 1 4 6 7 9 1 22 87 33  35 36 37 35 34] becomes [3 1 22 87 33 35 34].

How can one solve the same problem with 2 columns matrix (planar case)?

v=[1 1; 2 1; 2 2; 2 3; 3 3; 3 2; 2 2; 2 3; 2 4] should be [1 1; 2 1; 2 2; 2 3; 2 4]
sapienz
  • 152
  • 5
  • Convert subscripts to indices and us the linked solution? – beaker Jan 16 '17 at 22:00
  • I have a question about the problem definition. Limiting ourselves to 1-D vectors (for sake of brevity), what would you expect the output to be for input [1,2,3,4,3,5,4]. Would the output be [1,2,3] or [1,2,3,5,4]? The solution that you refer to above would result in the former, but it makes more sense to output the later IMO. – aksadv Jan 17 '17 at 03:42

1 Answers1

0

One of the answers in the thread that you linked in your question solves the problem for 1-D vectors. The 2-D array can be transformed into a 1-D complex vector (and back) using real-imaginarty to complex transform. Thus, the following could be a solution:

v=[1 1; 2 1; 2 2; 2 3; 3 3; 3 2; 2 2; 2 3; 2 4];
% from your example. note the change in variable name.

% convert the 2-D array into 1-D using real-imag to complex trasnform
u = v*[1;i];

Solution for the 1-D problem with trivial modifications from what's published here:

off = false;  % Faster to call function FALSE once
n   = length(u);
use = true(1, n);
i = 1;
while i <= n
    multi = find(u(i:n) == u(i), 1, 'last');
    use((i + 1):(i + multi - 1)) = off;
    i = i + multi;
end

Finally, select the selected rows from the input matrix:

v = v(use,:)
%  [1 1;  2 1;  2 2;  2 3;  2 4]
aksadv
  • 806
  • 5
  • 6
  • Thank you. Your 2d conversion to 1d complex format is an elegant way to find planar solution. Regarding your first comment, the expected output is of course [1,2,3,5,4]. – sapienz Jan 17 '17 at 16:38