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
endHello,
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 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 comparing values, arrays, for loops MATLAB Answers — New Questions