three level indexing and nested if statements

2 次查看(过去 30 天)
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