Clear Filters
Clear Filters

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

8 views (last 30 days)
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
Ammar
Ammar on 11 Jul 2024
Thank you for the response. Just to understand, why did you make the minimum distance infinity?
Umar
Umar on 11 Jul 2024
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

Divyajyoti Nayak
Divyajyoti Nayak on 11 Jul 2024
Edited: Divyajyoti Nayak on 11 Jul 2024
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
Ammar
Ammar on 11 Jul 2024
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

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!