import torch


def find_best_match_indices(search_for, search_in, less_or_equal=False, greater_or_equal=False):
    """For each element of `search_for`, find the index of closest element in `search_in`.

    Parameters
    ----------
    search_for: torch.tensor
    search_in: torch.tensor
    less_or_equal: bool
        If True, searches for an element which is less or equal than the reference from `search_for`
    greater_or_equal: bool
        If True, searches for an element which is greater or equal than the reference from `search_for`

    Returns
    -------
    indices: torch.Tensor
        Shape: `search_for.shape`
        If the required neighbour was not found, -1 value is used instead of true indices.
    """
    assert search_for.ndim <= 1, "search_for should be a scalar or 1D tensor."
    assert search_in.ndim == 1, "search_in should be a 1D tensor."
    diff = search_for.float().view(-1, 1) - search_in.float().view(1, -1)
    if less_or_equal:
        diff[diff < 0] = torch.tensor(float('inf')).to(search_in.device)  # pylint: disable=not-callable
    if greater_or_equal:
        diff[diff > 0] = torch.tensor(float('inf')).to(search_in.device)  # pylint: disable=not-callable
    diff = torch.abs(diff)
    res = torch.argmin(diff, dim=1)
    if less_or_equal or greater_or_equal:
        res[torch.all(diff == float('inf'), dim=1)] = -1
    return res if search_for.ndim else res.squeeze()