Comparing difference in values between values in an array - Using For Loop

Hello,
I have a question about comparing values between elements of an array. I keep getting the error that says
Index exceeds the number of array elements. Index must not exceed 1.
I attached my code but I also put the section of the code I'm having an issue with.
See MATLAB code attached:
E_Plane.m
theta = 0:0.01:pi/2
% The size of theta is 1 x 315
% E_Plane_norm_dB is an array that is also 1 x 315 with numbers
%{
Essentially, I am trying to see which number in the E_Plane_norm_dB array is the closest to -3.
Whichever number is closest to -3, I would need to output the "theta" value
that corresponds to that number in E_Plane_norm_dB
The way I'm doing this is by finding the "distance" from that element to -3
I keep comparing the "distances" to -3 between each element, until I find
the "smallest distance". Whichever element in E_Plane_norm_dB has the
smallest "distance", I would like to output that "theta" value.
%}
% Solving for the theta angle.
for j = 1:length(theta)
loop_theta = theta(j);
distance(j) = E_Plane_norm_dB(j) - (-3);
if (distance(j) < distance(j+1:length(theta)))
theta_angle = loop_theta;
end
end

4 Comments

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.

Hi Ammar,
As per the provided code, I understand that you are interested in finding the index of the element in array "E_Plane_norm_dB" which minimizes the difference "E_Plane_norm_dB - (-3);" and not the absolute distance as assumed in the previous comment, if this is the case, consider modifying your code as below-
differences = (E_Plane_norm_dB - (-3));
[~, index] = min(differences);
HPBW_Half_E=theta(index)
Thanks
Thank you for the response. Just to understand, why did you make the minimum distance infinity?
Hi Ammar,
Initializing the min_distance variable to inf (infinity) was to make sure that any calculated distance in the subsequent loop will be smaller than this initial value which will allow the comparison logic to correctly update the minimum distance as the loop progresses. Also,this approach guarantees that the initial distance comparison will always result in updating the minimum distance with the first calculated distance.

Sign in to comment.

 Accepted Answer

Hi @Ammar, the reason you are getting an error is because the distance variable is not pre defined as a vector so it is not able to index from j+1 to length(theta). You can verify this by looking at the distance variable in the workspace.
distance(j) = E_Plane_norm_dB(j) - (-3); %distance is not the complete vector as it has only one element
if (distance(j) < distance(j+1:length(theta))) %distance cannot index into j+1 as distance(j+1) is not defined yet
theta_angle = loop_theta;
To fix your code you can pre-allocate the distance vector with the zeros function, like this:
distance = zeros(length(theta), 1);
Or, you can use MATLAB's vector manipulation to get the desired result instead of a for loop.
theta = 0.0.01:pi/2;
distances = E_Plane_norm_dB - (-3);
[~,idx] = min(abs(distances));
theta_angle = theta(idx);

1 Comment

Thank You! The last section of code worked and gave the correct result.
theta = 0:0.01:pi/2;
distances = E_Plane_norm_dB - (-3);
[~,idx] = min(abs(distances));
theta_angle = theta(idx);

Sign in to comment.

More Answers (0)

Categories

Asked:

on 11 Jul 2024

Commented:

on 11 Jul 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!