필터 지우기
필터 지우기

conditional statement in one line for table columns

조회 수: 3 (최근 30일)
Priyanka Gaikwad
Priyanka Gaikwad 2017년 6월 23일
편집: Guillaume 2017년 6월 23일
logic: if(p2>0) r4=q2/p2 else r4=2*q2/p2 Question : how to convert this into r4={if p2>0?r4=q2/p2:r4=2*q2/p2} in matlab? p2 and q2 are columns of table.

채택된 답변

alice
alice 2017년 6월 23일
Have a look at the documentation, for example these: find-array-elements-that-meet-a-condition and element-wise-multiplication.
In your case, you can do:
r4 = (p2>0).*(q2./p2) + (p2<=0).*(q2./p2)*2;

추가 답변 (1개)

Guillaume
Guillaume 2017년 6월 23일
편집: Guillaume 2017년 6월 23일
Or, rather than relying on semi-obscure mathematical expressions:
r4 = q2 ./ p2;
r4(p2 > 0) = 2 * r4(p2 > 0);
In my opinion, a lot clearer as to the intent.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by