Problem with "if" command

조회 수: 3 (최근 30일)
Pietro Fiondella
Pietro Fiondella 2022년 7월 4일
댓글: Torsten 2022년 7월 4일
I want to remane and reassigne some variable according 2 different cases
For example consider this script:
%In my script PBtime_b and PBtime_nb are obtained from other calculaion,
%(here i just assige a value) but im interested in a comparision between
%them in order to select other values
PBtime_b=5;
PBtime_nb=6;
Ab=5;
Cb=6;
Anb=7;
Cnb=8;
if PBtime_nb<=PBtime_b;
A=Ab
C=Cb
end
if PBtime_b>PBtime_nb;
A=Anb
C=Cnb
end
A
Unrecognized function or variable 'A'.
C
X=A+C
I would obtain A=Anb=5 and C=Cnb=6 but if i type A or C the script dosent recognize the variables
what i am doing wrong?

채택된 답변

Pooja Kumari
Pooja Kumari 2022년 7월 4일
편집: Pooja Kumari 2022년 7월 4일
Hi Pierto
It is my understanding that you are facing "Unrecognized function or variable 'A'." error. And you want to rename and reassign some variable according to two different cases.
Please follow below updated code for the same in which you want to assign A=Anb= 5 and C= Cnb=6 as follows:
%In my script PBtime_b and PBtime_nb are obtained from other calculaion,
%(here i just assige a value) but im interested in a comparision between
%them in order to select other values
PBtime_b=5;
PBtime_nb=6;
Ab=5;
Cb=6;
Anb=7;
Cnb=8;
%you want A = Anb = 5 and C = Cnb = 6
if PBtime_nb>=PBtime_b;
A=Ab % Pbtime_nb = 6 > PBtime_b =5
C=Cb % A = 5, C = 6;
end
if PBtime_b>PBtime_nb;
Anb = A %Anb = 5
Cnb = C %Cnb = 6
end
A
C
X=A+C
For more infomation on variable Pre-allocation you can refer to the below link :
You can take help from workspace for checking correct variable assignment and you can refer to the link provided below:
Sincerely,
Pooja Kumari

추가 답변 (1개)

Torsten
Torsten 2022년 7월 4일
편집: Torsten 2022년 7월 4일
Should be
if PBtime_nb>PBtime_b
instead of
if PBtime_b>PBtime_nb;
  댓글 수: 2
DGM
DGM 2022년 7월 4일
편집: DGM 2022년 7월 4일
Since the cases appear to be mutually exclusive and non-independent, I don't see why the structure shouldn't be reduced to if-else.
if PBtime_nb<=PBtime_b;
A=Ab
C=Cb
else
A=Anb
C=Cnb
end
Torsten
Torsten 2022년 7월 4일
The OP's code with two separate if-statements is not wrong ...

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

카테고리

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

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by