excel if statement to matlab

조회 수: 14 (최근 30일)
Engineer Undergoing
Engineer Undergoing 2021년 7월 30일
댓글: Rik 2021년 8월 2일
hi
so i know there are many efieciant ways to do things on matlab without the need to use the if statement, and I was wondering if someone could help out on converting this excel formula/code to matlab without the need for a double if statements
=IF(AND(E5=0,E4<>0),F4+1,F4)

채택된 답변

Monika Jaskolka
Monika Jaskolka 2021년 7월 30일
편집: Monika Jaskolka 2021년 7월 30일
I don't see that a "double if" is necessary:
x = 0;
if (~E5 && E4)
x = F4 + 1;
else
x = F4;
end
If you are asking for a one line conditional assignment like below, Matlab doesn't support this syntax
condition ? true-expression : false-expression
  댓글 수: 6
Monika Jaskolka
Monika Jaskolka 2021년 8월 2일
Below is a direct translation, but if you are simply counting non-zero elements in E, you should look into the nnz command for a simpler solution.
% Init data
E = zeros(19,1);
E(15) = -0.333333;
E(16) = 0.333333;
F = ones(19,1);
F(17:end) = 2;
for i = 1:length(E)-1
if ~E(i+1) && E(i)
F(i) = F(i) + 1;
end
end
Rik
Rik 2021년 8월 2일
@Monika Jaskolka Consider teaching yourself to use numel instead of length. It is never a worse option, and it might save you from a difficult bug hiding in your program due to array input (instead of vector inputs).

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by