필터 지우기
필터 지우기

How to avoid the "if" statement

조회 수: 10 (최근 30일)
qingtao
qingtao 2013년 4월 17일
Hi,
I want to use a conditional term in an equation but don't want to use "if", simply because I want to explore something else.
Here is the problem,
E=A(2)*B+A(2)*C
where A=[a,b].
Here "a" is a flag. And I'd like to have:
If A(1)=1, E=A(2)*B+C
if A(2)=2, E=B+A(2)*C
How this can be realized without the "if" statement?
Thanks a lot

채택된 답변

Desiree Phillips
Desiree Phillips 2013년 4월 17일
편집: Desiree Phillips 2013년 4월 17일
Hi qingtao,
You mention 'a' is the flag, does that mean your second if statement should actually read:
if A(1)=2, E=B+A(2)*C
Either way, you can use logical statements as the coefficients for each equation. For example,
E = (A(1)==1)*(A(2)*B+C) + (A(1)==2)*(B+A(2)*C);
  댓글 수: 1
qingtao
qingtao 2013년 4월 17일
Thanks Desiree Phillips! This helps a lot

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by