Hi Ammar,

The main cause of the error was the incorrect usage of indexing within the loop. The comparison distance(j) < distance(j+1:length(theta)) is not valid as it compares a single element to an array slice, leading to the index error. However, I have fixed and updated the code as listed below along with attached test results.

>> theta = 0:0.01:pi/2;

E_Plane_norm_dB = randn(size(theta)); % Example initialization

% Initialize variables to store the minimum distance and corresponding theta value

min_distance = inf;

theta_angle = 0;

% Iterate over each element in the arrays

for j = 1:length(theta)

% Calculate the distance from the current element to -3

current_distance = abs(E_Plane_norm_dB(j) - (-3));

% Check if the current distance is smaller than the minimum distance found so far

if current_distance < min_distance

% Update the minimum distance and corresponding theta value min_distance = current_distance; theta_angle = theta(j); end end

% Output the theta value corresponding to the element closest to -3

disp(['The theta value corresponding to the element closest to -3 is: ', num2str(theta_angle)]);

Attached result:

Please let me know if I can assist you further. Hope this will help resolve your problem.