three level indexing and nested if statements

조회 수: 2 (최근 30일)
whynotwegs
whynotwegs 2017년 1월 13일
편집: Stephen23 2017년 1월 13일
I have an index with three conditions, one condition in each column. I want to create a master index with one column for classifications which incorporate all three columns of the previous index. I did this successfully with two conditions but can't figure out how to add the third. I include the code that works and my attempt to change it.
for i = 1:length(DEFcomboIDX)
if DEFcomboIDX(i,1) % Type 1 SN %If comboIDX column 1 = 1 then Type 1
if ~DEFcomboIDX(i,2) % If comboIDX column 2 =0 then SN
DEFindex(i,1) = 1; % mark it with a 1
elseif DEFcomboIDX(i,2) % Type 1 VTA
DEFindex(i,1) = 2; % if comboIDX column 2 = 1 then VTa; Mark with a 2
end
elseif ~DEFcomboIDX(i,1) % Type 2 SN
if ~DEFcomboIDX(i,2)
DEFindex(i,1) = 3;
elseif DEFcomboIDX(i,2) % Type 2 VTA
DEFindex(i,1) = 4;
end
end
end
and what i'm trying now:
for i = 1:length(DEFcomboIDX)
if DEFcomboIDX(i,1) % Type 1 SN %If comboIDX column 1 = 1 then Type 1
if ~DEFcomboIDX(i,2) % If comboIDX column 2 =0 then SN
if DEFcomboIDX(i,3)
DEFindex(i,1) = 1; % mark it with a 1
elseif DEFcomboIDX(i,2) % Type 1 VTA
if DEFcomboIDX(i,3)
DEFindex(i,1) = 2; % if comboIDX column 2 = 1 then VTa; Mark with a 2
end
elseif ~DEFcomboIDX(i,1) % Type 2 SN
if ~DEFcomboIDX(i,2)
if DEFindex(i,1) = 3;
elseif DEFcomboIDX(i,2) % Type 2 VTA
DEFindex(i,1) = 4;
end
end
end
end
end
end
  댓글 수: 1
Stephen23
Stephen23 2017년 1월 13일
편집: Stephen23 2017년 1월 13일
@whynotwegs: Using a truth table would be much simpler than this code. You could do this in just a few lines of neater and more robust code.

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

채택된 답변

Walter Roberson
Walter Roberson 2017년 1월 13일
In your first code, the elseif for Type 2 SN is matched with the if for Type 1 SN . In your second code, the elseif for Type 2 SN is matched with the if for Type 1 VTA. I think you are missing an "end"
  댓글 수: 4
whynotwegs
whynotwegs 2017년 1월 13일
Actually this still doesnt work. The rows that meet the first two conditions and not the third aren't being classified. I tried to add more elseifs to reroute them but it didn't work. I made sure to match the proper if with the proper elseif and match up my ends
for i = 1:length(DEFcomboIDX)
if DEFcomboIDX(i,1) % Type 1 SN %If comboIDX column 1 = 1 then Type 1
if DEFcomboIDX(i,3) % ANd if combo IDX column 3 =1 then meets baseline requirements
if ~DEFcomboIDX(i,2) % If comboIDX column 2 =0 then SN
DEFindex(i,1) = 1;
elseif DEFcomboIDX(i,2) % Type 1 VTA
DEFindex(i,1) = 2; % if comboIDX column 2 = 1 then VTa; Mark with a 2
end
elseif ~DEFcomboIDX(1,3)
if ~DEFcomboIDX(i,2)
DEFindex(i,1) = 3;
elseif DEFcomboIDX(1,2)
DEFindex(i,1) = 4;
end
end
elseif ~DEFcomboIDX(i,1) % Type 2 SN
if ~DEFcomboIDX(i,2)
DEFindex(i,1) = 3;
elseif DEFcomboIDX(i,2) % Type 2 VTA
DEFindex(i,1) = 4;
end
end
end
Walter Roberson
Walter Roberson 2017년 1월 13일
You coded
elseif ~DEFcomboIDX(1,3)
instead of
elseif ~DEFcomboIDX(i,3)
Might I suggest you build a truth table?
1 3 ~2 => value 1
1 3 2 => value 2
1 ~3 ~2 => value 3
1 ~3 2 => value 4
~1 ~2 => value 3
~1 2 => value 4
Is that the complete table? If it is, then
0 0 0 => value 3
0 0 1 => value 3
0 1 0 => value 4
0 1 1 => value 4
1 0 0 => value 3
1 0 1 => value 1
1 1 0 => value 4
1 1 1 => value 2
And that can be expressed as:
value_table = [3 3 4 4 3 1 4 2];
value_table( (DEFcomboIDX(i,:) * [4; 2; 1]) + 1 )
The * [4; 2; 1] converts the triple from binary into decimal 0 to 7, add 1 to get an index into the table.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by