Make if statement more readable

조회 수: 1 (최근 30일)
Joel Schelander
Joel Schelander 2021년 4월 15일
댓글: Rik 2021년 4월 20일
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