Clear Filters
Clear Filters

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

3 views (last 30 days)
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 Comment
Mathieu NOE
Mathieu NOE on 16 Mar 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

Sign in to comment.

Accepted Answer

Cameron
Cameron on 16 Mar 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

More Answers (0)

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!