Make if statement more readable
1 view (last 30 days)
Show older comments
I have an if statement that looks like this
if numel(intersect(VID1{index(1)},VID2{index(2)}))||numel(intersect(VID2{index(2)},VID3{index(3)}))||numel(intersect(VID1{index(1)},VID3{index(3)}))|| numel(intersect(VID4{index(4)},VID1{index(1)}))||numel(intersect(VID4{index(4)},VID2{index(2)}))||numel(intersect(VID4{index(4)},VID3{index(3)}))||numel(intersect(VID1{index(1)},VID5{index(5)}))||numel(intersect(VID2{index(2)},VID5{index(5)}))||numel(intersect(VID5{index(5)},VID3{index(3)}))|| numel(intersect(VID4{index(4)},VID5{index(5)}))||numel(intersect(VID1{index(1)},VID6{index(6)}))||numel(intersect(VID2{index(2)},VID6{index(6)}))||numel(intersect(VID6{index(6)},VID3{index(3)}))|| numel(intersect(VID4{index(4)},VID6{index(6)}))||numel(intersect(VID5{index(5)},VID6{index(6)}))||numel(intersect(VID1{index(1)},VID7{index(7)}))||numel(intersect(VID2{index(2)},VID7{index(7)}))||numel(intersect(VID7{index(7)},VID3{index(3)}))|| numel(intersect(VID4{index(4)},VID7{index(7)}))||numel(intersect(VID5{index(5)},VID7{index(7)}))||numel(intersect(VID6{index(6)},VID7{index(7)}))
continue
end
I wonder if I can make this more readable by e.g. moving down a line, but if I do that now that interrupts the if statement
3 Comments
Rik
on 20 Apr 2021
It would not speed up the run time, but it will speed up development. Currently you have several VID_ arrays, where you need to check if you have tested all combinations of VID_ and index.
Whenever you find yourself writing variable names ending in a counter, stop to think if you can replace it with indexed variables. Most occurence that would mean replacing VID1 with VID{1}, but in this if statement you can probably procedurally generate the required combinations. As long as you still use ||, it will maintain reasonable performance.
L=false;
for n=1:10, L= L || fun(n); end
Accepted Answer
David Hill
on 15 Apr 2021
if numel(intersect(VID1{index(1)},VID2{index(2)}))||...
numel(intersect(VID2{index(2)},VID3{index(3)}))||...
numel(intersect(VID1{index(1)},VID3{index(3)}))||...
numel(intersect(VID4{index(4)},VID1{index(1)}))||...
numel(intersect(VID4{index(4)},VID2{index(2)}))||...
numel(intersect(VID4{index(4)},VID3{index(3)}))||...
numel(intersect(VID1{index(1)},VID5{index(5)}))||...
numel(intersect(VID2{index(2)},VID5{index(5)}))||...
numel(intersect(VID5{index(5)},VID3{index(3)}))||...
numel(intersect(VID4{index(4)},VID5{index(5)}))||...
numel(intersect(VID1{index(1)},VID6{index(6)}))||...
numel(intersect(VID2{index(2)},VID6{index(6)}))||...
numel(intersect(VID6{index(6)},VID3{index(3)}))||...
numel(intersect(VID4{index(4)},VID6{index(6)}))||...
numel(intersect(VID5{index(5)},VID6{index(6)}))||...
numel(intersect(VID1{index(1)},VID7{index(7)}))||...
numel(intersect(VID2{index(2)},VID7{index(7)}))||...
numel(intersect(VID7{index(7)},VID3{index(3)}))||...
numel(intersect(VID4{index(4)},VID7{index(7)}))||...
numel(intersect(VID5{index(5)},VID7{index(7)}))||...
numel(intersect(VID6{index(6)},VID7{index(7)}))
continue;
end
0 Comments
More Answers (0)
See Also
Categories
Find more on Get Started with MATLAB 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!