Make if statement more readable

1 次查看(过去 30 天)
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 个评论
Joel Schelander
Joel Schelander 2021-4-20
How would such an array look like @Rik? Would it speed things up, with a loop?
Rik
Rik 2021-4-20
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

请先登录,再进行评论。

采纳的回答

David Hill
David Hill 2021-4-15
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 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by