diff --git a/Python/waveform.py b/Python/waveform.py
index fcb0c8157911e6f378ce8f31caa64e9161ceddd3..7cbbb11a0d6912ddb3032d68aba6004ee32c6e2c 100644
--- a/Python/waveform.py
+++ b/Python/waveform.py
@@ -4,6 +4,7 @@ import numpy as np
 from scipy.interpolate import interp1d
 from AWG import *
 import cupy as cp
+import scipy.optimize as spopt
 
 
 class Waveform:
@@ -318,26 +319,9 @@ def get_rearrange_paths(
     :param t_idx: indices of tweezer positions in target pattern.
     :returns: 2d numpy array containing moving path trajectories
     """
-    if len(f_idx) < len(t_idx):
-        return np.array([])
-    l_ptr = np.searchsorted(f_idx, t_idx[0])
-    r_ptr = np.searchsorted(f_idx, t_idx[-1], side='right') - 1
-    n_unpaired = len(t_idx) - len(f_idx[l_ptr:r_ptr+1])
-    while n_unpaired > 0:
-        if l_ptr == 0:
-            r_ptr += n_unpaired
-            break
-        if r_ptr == len(f_idx) - 1:
-            l_ptr -= n_unpaired
-            break
-        l_dist = abs(t_idx[0] - f_idx[l_ptr - 1])
-        r_dist = abs(f_idx[r_ptr + 1] - t_idx[-1])
-        if l_dist < r_dist:
-            l_ptr -= 1
-        else:
-            r_ptr += 1
-        n_unpaired -= 1
-    return np.vstack((f_idx[l_ptr:r_ptr+1], t_idx)).T
+    cm = abs(np.subtract.outer(f_idx, t_idx))
+    row, col = spopt.linear_sum_assignment(cm)
+    return np.stack([f_idx[row], t_idx]).T
 
 
 def create_moving_array_old(path_table: np.ndarray, paths: np.ndarray) -> np.ndarray: