Make if statement more readable

1 visualización (últimos 30 días)
Joel Schelander
Joel Schelander el 15 de Abr. de 2021
Comentada: Rik el 20 de Abr. de 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 comentarios
Joel Schelander
Joel Schelander el 20 de Abr. de 2021
How would such an array look like @Rik? Would it speed things up, with a loop?
Rik
Rik el 20 de Abr. de 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

Iniciar sesión para comentar.

Respuesta aceptada

David Hill
David Hill el 15 de Abr. de 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

Más respuestas (0)

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Productos


Versión

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by