why If statement is not work?
Mostrar comentarios más antiguos
eq1 have at each nf the values 0.186170065500000 0.336133515500000 0.486096965500000 0.636060415500000 0.786023865500000 0.935987315500000 1.08595076550000
i want to plot out1 when eq1 >1so, out1 should be calculated at 1.08595076550000
eq1=((bwf*nf)./wi)+((bwm*nm1)./wi);
out1=(bwf*nf)./wi;
plot (eq1);
hold on
if all(eq1 >=1)
out1=(bwf*nf)./wi;
plot (out1);
end
1 comentario
Adam
el 22 de Nov. de 2018
To answer the question in the title, read the logic of your if statement.
if all( eq1 >= 1 )
i.e. it will only evaluate to true if all the values in eq1 are greater than or equal to 1. The result is a scalar logical, as needed generally for an if statement, but it isn't capturing what you want.
Respuesta aceptada
Más respuestas (1)
Image Analyst
el 22 de Nov. de 2018
The link about comparing equality of floating point numbers is good information, though it doesn't apply here since none of your eq1 numbers are expected to be exactly 1 - they're all clearly either more than or less than 1.
Your original code
wi=10000000;
nf=1:7;
nm1=1;
bwf=1499634.50;
bwm=362066.155;
eq1=((bwf*nf)./wi)+((bwm*nm1)./wi);
out1=(bwf*nf)./wi;
plot (eq1);
hold on
if all(eq1 >=1)
out1=(bwf*nf)./wi;
plot (out1);
end
did plot the first plot. It did not plot the second plot since not ALL elements of eq1 were more than 1. I believe you want masking, where you plot only those elements where eq1 > 1 and no others ("i want to plot out1 when eq1 >1"). To do that, see this corrected code:
wi=10000000;
nf=1:7;
nm1=1;
bwf=1499634.50;
bwm=362066.155;
eq1=((bwf*nf)./wi)+((bwm*nm1)./wi)
% Plot eq1.
plot (eq1, 'bo-');
xlabel('index', 'FontSize', 15);
ylabel('eq1', 'FontSize', 15);
grid on;
% Compute out1.
out1 = (bwf * nf) ./ wi % Note: same as
% Create a mask where only those where eq1 > 1 are true.
mask = eq1 > 1
% Plot only those where eq1 > 1 (where the mask is true).
hold on;
x = 1 : length(out1)
plot (x(mask), out1(mask), 'r*', 'MarkerSize',13, 'LineWidth', 2);
legend('eq1', 'out1', 'Location', 'north');

Your original eq1 (outside the if block) is plotted in blue while the points of out1, only where eq1 is greater than 1, are plotted in red.
1 comentario
maryhan mohamed
el 22 de Nov. de 2018
Categorías
Más información sobre Point Cloud Processing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!