# Why if statement with odd and even number does not work in function and just skipped condition?

4 visualizaciones (últimos 30 días)
Aknur el 16 de Mzo. de 2023
Comentada: Aknur el 16 de Mzo. de 2023
Hello everyone! Does someone know why this code
if mod(plane,2) == 0 %even
ThetaBar = Theta0;
fprintf('ThetaBar: %f\n',ThetaBar);
else
ThetaBar = 90 - Theta0; %odd
fprintf('ThetaBar: %f\n',ThetaBar);
end
does not work inside of function properly. Idea is if plane if odd then ThetaBar = 90 - Theta0, else if plane is even then ThetaBar = Theta0. I run two function in main. But it does not calculate ThetaBar(ii) as I expect. It seems like code just skipped my statement.
Thank you in advance. Appreciate any help
function [p, Xr, Yr, Zr, ThetaBar, PhiBar, tempPlane] = planeLocation5(XBar, YBar, ZBar,X0,Y0,Z0,Theta0, Phi0)
planes(:,:,1) = [0 3 3; 0 0 3; 0 3 0; 0 0 0; 0 0 0];
planes(:,:,2) = [0 0 3; 3 0 3; 0 0 0; 3 0 0; 0 0 0];
planes(:,:,3) = [3 0 3; 3 3 3; 3 0 0; 3 3 0; 3 0 0];
planes(:,:,4) = [3 3 3; 0 3 3; 3 3 0; 0 3 0; 0 3 3];
planes(:,:,5) = [0 3 0; 3 3 0; 0 0 0; 3 0 0; 0 0 0];
planes(:,:,6) = [0 3 3; 3 3 3; 0 0 3; 3 0 3; 0 0 3];
location_plane = 6;
....
if mod(plane,2) == 0 %even
ThetaBar = Theta0;
fprintf('ThetaBar: %f\n',ThetaBar);
else
ThetaBar = 90 - Theta0; %odd
fprintf('ThetaBar: %f\n',ThetaBar);
end
and then I run in main
clc
clear all
close all
X0 = 1.5;
Y0 = 1.5;
Z0 = 3.0;
Theta0 = 60;
Phi0 = 90;
K = 5;
X = [X0 zeros(1,K)];
Y = [Y0 zeros(1,K)];
Z = [Z0 zeros(1,K)];
Theta = [Theta0 zeros(1,K)];
Phi = [Phi0 zeros(1,K)];
for ii = 1:K
ii
X(ii)
Y(ii)
Z(ii)
Theta(ii)
Phi(ii)
[XBar, YBar, ZBar] = reflection5(X(ii), Y(ii), Z(ii), Theta(ii), Phi(ii));
[p, Xr, Yr, Zr, ThetaBar, PhiBar, tempPlane] = planeLocation5(XBar, YBar, ZBar,X(ii), Y(ii), Z(ii),Theta(ii), Phi(ii));
%if plane == 0 || plane == 2 || plane == 4 %even
% ThetaBar = Theta(ii);
%else
% ThetaBar = 90 - Theta(ii); %odd
%end
X(ii+1) = Xr;
Y(ii+1) = Yr;
Z(ii+1) = Zr;
Theta(ii+1) = ThetaBar;
Phi(ii+1) = PhiBar;
end
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Mathieu NOE el 16 de Mzo. de 2023
hello
function reflection5 is missing, we cannot fully test your code
in function planeLocation5 , we do not see where the variable plane comes from , so again we cannot test why the odd / even test fails

Iniciar sesión para comentar.

Cameron el 16 de Mzo. de 2023
I suspect what's happening is that you're passing a vector or array into your mod() function. If all elements of your variable "plane" are not even, it will send you to the else portion of your code. See this for example
x = [2,3;2,2]; %all these numbers are not even
if mod(x,2) == 0
disp('All numbers in array are even')
else
disp('All numbers in array are not even')
end
All numbers in array are not even
%and compare it with this
x2 = [2,2;2,2]; %all these numbers are not even
if mod(x2,2) == 0
disp('All numbers in array are even')
else
disp('All numbers in array are not even')
end
All numbers in array are even
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Aknur el 16 de Mzo. de 2023
Dear, @Cameron thank you it works now. I also mixed up odd and even

Iniciar sesión para comentar.

### Categorías

Más información sobre Multidimensional Arrays en Help Center y File Exchange.

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by