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:
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.
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
% 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 个评论
Umar
2024-7-11
编辑:Walter Roberson
2026-11-12,20:02
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:
<</matlabcentral/answers/uploaded_files/1732158/IMG_7033.jpeg>>
Please let me know if I can assist you further. Hope this will help resolve your problem.
VINAYAK LUHA
2024-7-11
编辑:VINAYAK LUHA
2024-7-11
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
N/A
2024-7-11
Hi,
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.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Quadratic Programming and Cone Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!