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

조회 수: 4 (최근 30일)
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
Mathieu NOE
Mathieu NOE 2023년 3월 16일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Cameron
Cameron 2023년 3월 16일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by