Make if statement more readable

1 vue (au cours des 30 derniers jours)
Joel Schelander
Joel Schelander le 15 Avr 2021
Commenté : Rik le 20 Avr 2021
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 commentaires
Joel Schelander
Joel Schelander le 20 Avr 2021
How would such an array look like @Rik? Would it speed things up, with a loop?
Rik
Rik le 20 Avr 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

Connectez-vous pour commenter.

Réponse acceptée

David Hill
David Hill le 15 Avr 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

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by