Problem with Nested if statements

조회 수: 6 (최근 30일)
Aftab Ahmed Khan
Aftab Ahmed Khan 2015년 11월 9일
편집: Aftab Ahmed Khan 2015년 11월 9일
Hi everyone, I am using nested if statements to perform a subsection of my project but i don't like the way i have implemented it. I am sure that there will be some optimum way of doing the following task, so to avoid so many if statements. Thank you so much.
if any(idcabs==[1 2 3 4])
if any(idabs==[1 2 5 6 9 10 13 14])
idbeam1=1;
else
idbeam1=3;
end
elseif any(idcabs==[5 6 7 8])
if any(idabs==[1 2 5 6 9 10 13 14])
idbeam1=3;
else
idbeam1=1;
end
elseif any(idcabs==[9 10 11 12])
if any(idabs==[1 2 3 4 5 6 7 8])
idbeam1=2;
else
idbeam1=4;
end
elseif any(idcabs==[13 14 15 16])
if any(idabs==[1 2 3 4 5 6 7 8])
idbeam1=4;
else
idbeam1=2;
end
end

채택된 답변

Walter Roberson
Walter Roberson 2015년 11월 9일
You should consider using ismember()
  댓글 수: 2
Aftab Ahmed Khan
Aftab Ahmed Khan 2015년 11월 9일
편집: Aftab Ahmed Khan 2015년 11월 9일
Hi Walter, even if a consider ismember(), i will still need those nested If statements. It will be just a change of functions from any() to ismemeber(). Do you mean something like this just for the first part.
Aftab Ahmed Khan
Aftab Ahmed Khan 2015년 11월 9일
id1=ismember(idcabs,C1)
id2=ismember(idabs,S1)
if ~isempty(id1) && ~isempty(id2)
idbeam1=1;
else
idbeam1=3;
end

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by