three level indexing and nested if statements
2 views (last 30 days)
Show older comments
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 Comment
Accepted Answer
Walter Roberson
on 13 Jan 2017
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 Comments
Walter Roberson
on 13 Jan 2017
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.
More Answers (0)
See Also
Categories
Find more on Downloads in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!