function [filtered_theta,selectedIndices] = theta_pmf2(pmf,n,Theta_)

A = pmf;
%i = 1:length(Theta_);

% Sort the elements of A in descending order along with their indices
[sortedA, sortedIndices] = sort(A, 'descend');

% Initialize variables to store selected elements and their indices
selectedA = [];
selectedIndices = [];

% Iterate through the sorted elements
for j = 1:numel(sortedA)
    % Check if the current element is valid to be selected
    if ~any(abs(selectedIndices - sortedIndices(j)) <= 20)
        % If valid, add it to the selected elements
        selectedA = [selectedA, sortedA(j)];
        selectedIndices = [selectedIndices, sortedIndices(j)];
        
        % Check if you have selected 3 elements
        if numel(selectedA) == n
            break; % Stop the loop if 3 elements are selected
        end
    end
end

% Display the selected elements and their indices
% disp('Selected Elements:');
% disp(selectedA);
% disp('Corresponding Indices:');
% disp(i(selectedIndices));
filtered_theta = Theta_(selectedIndices);
filtered_theta = sort(Theta_(selectedIndices));

end